forked from PsionicGeek/backend-food-ordering-system
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from PsionicGeek/master
new commit
- Loading branch information
Showing
164 changed files
with
7,511 additions
and
8,376 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
/node_modules/ | ||
node_modules | ||
.env |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,54 +1,30 @@ | ||
var createError = require('http-errors'); | ||
var express = require('express'); | ||
var path = require('path'); | ||
var cookieParser = require('cookie-parser'); | ||
var logger = require('morgan'); | ||
|
||
var indexRouter = require('./routes/index'); | ||
var usersRouter = require('./routes/users'); | ||
var adminRouter = require('./routes/admin'); | ||
|
||
var app = express(); | ||
//======================================================================================================================= | ||
const express = require('express') | ||
const mongoose = require('mongoose') | ||
const dotenv = require('dotenv') | ||
dotenv.config({ path: './env' }) | ||
//========================================================================================================================== | ||
// view engine setup | ||
app.set('views', path.join(__dirname, 'views')); | ||
app.set('view engine', 'ejs'); | ||
|
||
app.use(logger('dev')); | ||
const app = express() | ||
const dotenv = require('dotenv'); | ||
const userRouter = require("./routes/user") | ||
dotenv.config({path : '.env'}) | ||
|
||
//================================================================================= | ||
app.use(express.json()); | ||
app.use(express.urlencoded({ extended: false })); | ||
app.use(cookieParser()); | ||
app.use(express.static(path.join(__dirname, 'public'))); | ||
const adminRouter = require('./routes/admin') | ||
|
||
|
||
//================================================================================== | ||
|
||
app.use('/', indexRouter); | ||
app.use('/users', usersRouter); | ||
app.use('/admin', adminRouter); | ||
app.use('/user', userRouter); | ||
|
||
|
||
//================================================================================= | ||
//CONNECT TO DATABASE | ||
|
||
// catch 404 and forward to error handler | ||
app.use(function (req, res, next) { | ||
next(createError(404)); | ||
}); | ||
|
||
// error handler | ||
app.use(function (err, req, res, next) { | ||
// set locals, only providing error in development | ||
res.locals.message = err.message; | ||
res.locals.error = req.app.get('env') === 'development' ? err : {}; | ||
|
||
// render the error page | ||
res.status(err.status || 500); | ||
res.render('error'); | ||
}); | ||
//==================================================================================================================== | ||
//CONNECTION WITH THE DATABASE | ||
mongoose.connect(process.env.DB_URL, { useNewUrlParser: true, useUnifiedTopology: true }) | ||
mongoose.connect( "mongodb://localhost:27017/TastyDB") | ||
.then(() => { console.log('CONNECTED TO DATABASE :)') }) | ||
.catch((err) => { console.log('CONNECTION TO DATABASE FAILED :(', err) }) | ||
|
||
|
||
//========================================================================================================================== | ||
module.exports = app; | ||
//=========================================================================== | ||
app.listen(8000, ()=>{ | ||
console.log('Server Started at port 8000') | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
const asyncHandler = require("express-async-handler"); | ||
const User = require("../models/userSchema"); | ||
const jwt = require("jsonwebtoken"); | ||
const bcrypt = require('bcrypt'); | ||
|
||
// controllers/authController.js | ||
const signup = async (req, res) => { | ||
try { | ||
const { username, email, mobileNumber, address, password } = req.body; | ||
|
||
// Check if user with the given email or mobile number already exists | ||
const existingUser = await User.findOne({ $or: [{ email }, { mobileNumber }] }); | ||
|
||
if (existingUser) { | ||
return res.status(400).json({ message: 'User with this email or mobile number already exists' }); | ||
} | ||
|
||
// Hash the password | ||
const hashedPassword = await bcrypt.hash(password, 10); | ||
|
||
// Create a new user | ||
const newUser = new User({ | ||
username, | ||
email, | ||
mobileNumber, | ||
address, | ||
password: hashedPassword, // Save the hashed password | ||
}); | ||
|
||
// Save the user to the database | ||
await newUser.save(); | ||
|
||
res.status(201).json({ message: 'User registered successfully' }); | ||
} catch (error) { | ||
console.error(error); | ||
res.status(500).json({ message: 'Internal Server Error' }); | ||
} | ||
}; | ||
|
||
|
||
const signin= (req, res) =>{ | ||
console.log("hello"); | ||
res.send("Hello") | ||
}; | ||
module.exports = authController = { | ||
signup, | ||
signin | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
const jwt = require("jsonwebtoken"); | ||
|
||
const requireSign = (req, res, next)=>{ | ||
if(req.headers.authorization){ | ||
const token = req.headers.authorization.split(" ")[1]; | ||
const user = jwt.verify(token, process.env.JWT_SECRET); | ||
req.user=user; | ||
} else { | ||
return res | ||
.status(401) | ||
.json({error:"authorization required"}); | ||
} | ||
next(); | ||
}; | ||
|
||
const userMiddleware = (req, res, next) =>{ | ||
if (req.user.isAdmin==true){ | ||
return res | ||
.status(401) | ||
.json({error:"User Acess Denied"}) | ||
} | ||
next(); | ||
}; | ||
|
||
const adminMiddleware = (req, res, next) => { | ||
if(req.user.isAdmin==false){ | ||
return res | ||
.status(401) | ||
.json({error:"Not authorised as an admin"}) | ||
} | ||
next(); | ||
} | ||
|
||
module.exports = commonMiddleware = { | ||
requireSign, | ||
userMiddleware, | ||
adminMiddleware | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.