Skip to content

Commit

Permalink
Added signup and sign in functionality, hashing passwords using bcryp…
Browse files Browse the repository at this point in the history
…tjs and protecting routes using the auth middleware
  • Loading branch information
rituparna-ui committed Jul 25, 2022
1 parent 4ffd5f8 commit 3aa3f35
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 35 deletions.
12 changes: 0 additions & 12 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,18 +57,6 @@ app.use(errorController.get404);
mongoose
.connect(MONGODB_URI)
.then(result => {
User.findOne().then(user => {
if (!user) {
const user = new User({
name: 'Ritu',
email: '[email protected]',
cart: {
items: []
}
});
user.save();
}
});
app.listen(3000);
})
.catch(err => {
Expand Down
60 changes: 52 additions & 8 deletions controllers/auth.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const bcrypt = require('bcryptjs');

const User = require('../models/user');

exports.getLogin = (req, res, next) => {
Expand All @@ -17,19 +19,61 @@ exports.getSignup = (req, res, next) => {
};

exports.postLogin = (req, res, next) => {
User.findById('5bab316ce0a7c75f783cb8a8')
const email = req.body.email;
const password = req.body.password;
User.findOne({ email: email })
.then(user => {
req.session.isLoggedIn = true;
req.session.user = user;
req.session.save(err => {
console.log(err);
res.redirect('/');
});
if (!user) {
return res.redirect('/login');
}
bcrypt
.compare(password, user.password)
.then(doMatch => {
if (doMatch) {
req.session.isLoggedIn = true;
req.session.user = user;
return req.session.save(err => {
console.log(err);
res.redirect('/');
});
}
res.redirect('/login');
})
.catch(err => {
console.log(err);
res.redirect('/login');
});
})
.catch(err => console.log(err));
};

exports.postSignup = (req, res, next) => {};
exports.postSignup = (req, res, next) => {
const email = req.body.email;
const password = req.body.password;
const confirmPassword = req.body.confirmPassword;
User.findOne({ email: email })
.then(userDoc => {
if (userDoc) {
return res.redirect('/signup');
}
return bcrypt
.hash(password, 12)
.then(hashedPassword => {
const user = new User({
email: email,
password: hashedPassword,
cart: { items: [] }
});
return user.save();
})
.then(result => {
res.redirect('/login');
});
})
.catch(err => {
console.log(err);
});
};

exports.postLogout = (req, res, next) => {
req.session.destroy(err => {
Expand Down
6 changes: 6 additions & 0 deletions middleware/is-auth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
module.exports = (req, res, next) => {
if (!req.session.isLoggedIn) {
return res.redirect('/login');
}
next();
}
4 changes: 2 additions & 2 deletions models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ const mongoose = require('mongoose');
const Schema = mongoose.Schema;

const userSchema = new Schema({
name: {
email: {
type: String,
required: true,
},
email: {
password: {
type: String,
required: true,
},
Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
"nodemon": "^1.18.3"
},
"dependencies": {
"bcryptjs": "^2.4.3",
"body-parser": "^1.18.3",
"connect-mongodb-session": "^2.0.3",
"ejs": "^2.6.1",
Expand Down
13 changes: 7 additions & 6 deletions routes/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,23 @@ const path = require('path');
const express = require('express');

const adminController = require('../controllers/admin');
const isAuth = require('../middleware/is-auth');

const router = express.Router();

// /admin/add-product => GET
router.get('/add-product', adminController.getAddProduct);
router.get('/add-product', isAuth, adminController.getAddProduct);

// /admin/products => GET
router.get('/products', adminController.getProducts);
router.get('/products', isAuth, adminController.getProducts);

// /admin/add-product => POST
router.post('/add-product', adminController.postAddProduct);
router.post('/add-product', isAuth, adminController.postAddProduct);

router.get('/edit-product/:productId', adminController.getEditProduct);
router.get('/edit-product/:productId', isAuth, adminController.getEditProduct);

router.post('/edit-product', adminController.postEditProduct);
router.post('/edit-product', isAuth, adminController.postEditProduct);

router.post('/delete-product', adminController.postDeleteProduct);
router.post('/delete-product', isAuth, adminController.postDeleteProduct);

module.exports = router;
11 changes: 6 additions & 5 deletions routes/shop.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const path = require('path');
const express = require('express');

const shopController = require('../controllers/shop');
const isAuth = require('../middleware/is-auth');

const router = express.Router();

Expand All @@ -12,14 +13,14 @@ router.get('/products', shopController.getProducts);

router.get('/products/:productId', shopController.getProduct);

router.get('/cart', shopController.getCart);
router.get('/cart', isAuth, shopController.getCart);

router.post('/cart', shopController.postCart);
router.post('/cart', isAuth, shopController.postCart);

router.post('/cart-delete-item', shopController.postCartDeleteProduct);
router.post('/cart-delete-item', isAuth, shopController.postCartDeleteProduct);

router.post('/create-order', shopController.postOrder);
router.post('/create-order', isAuth, shopController.postOrder);

router.get('/orders', shopController.getOrders);
router.get('/orders', isAuth, shopController.getOrders);

module.exports = router;
4 changes: 2 additions & 2 deletions views/auth/signup.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<%- include('../includes/navigation.ejs') %>

<main>
<form class="login-form" action="/login" method="POST">
<form class="login-form" action="/signup" method="POST">
<div class="form-control">
<label for="email">E-Mail</label>
<input type="email" name="email" id="email">
Expand All @@ -20,7 +20,7 @@
<label for="confirmPassword">Confirm Password</label>
<input type="password" name="confirmPassword" id="confirmPassword">
</div>
<button class="btn" type="submit">Login</button>
<button class="btn" type="submit">Signup</button>
</form>
</main>
<%- include('../includes/end.ejs') %>

0 comments on commit 3aa3f35

Please sign in to comment.