Skip to content

Commit 41c21f5

Browse files
committed
Do more code on authentication part, 1.after signed in restrict user to sign in again 2. suppose a user is signed in and the user somehow get the login page or open in a another tab then when he try to signed in again then i restrict them using middleware named as secure 3. work on user perosonilasation mease a specfic user can see their own expenses or data only user edit or delete their own data only create data with their specified id,i acchive this by using foreign key in expense table (Now, all authentication part is done)
1 parent e697b8d commit 41c21f5

File tree

4 files changed

+35
-11
lines changed

4 files changed

+35
-11
lines changed

controllers/expense_controller.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ module.exports.addExpense = async (req, res) => {
2424
date: date,
2525
category: category,
2626
description: description,
27-
amount: amount
28-
// userId: req.user.id
27+
amount: amount,
28+
userId: req.user.id
2929
}
3030
// ,{ transaction: t }
3131
).then((data) => {
@@ -44,7 +44,7 @@ module.exports.addExpense = async (req, res) => {
4444

4545
exports.getAllExpenses = async (req, res) => {
4646
try {
47-
const expenses = await Expense.findAll(); //{ where: { userId: req.user.id } }
47+
const expenses = await Expense.findAll({ where: { userId: req.user.id } });
4848
res.json(expenses); // send the date where api call (This is is API or controller for get expense data)
4949
} catch (err) {
5050
console.log(err);
@@ -61,7 +61,7 @@ exports.deleteExpense = async (req, res) => {
6161
// },
6262
// { where: { id: req.user.id } }
6363
// );
64-
await Expense.destroy({ where: { id: id } }); //,userId: req.user.id
64+
await Expense.destroy({ where: { id: id } });
6565
res.redirect("/user_dashboard");
6666
} catch (err) {
6767
console.log(err);
@@ -88,7 +88,7 @@ exports.updateExpense = async (req, res) => {
8888
category: category,
8989
description: description,
9090
amount: amount
91-
}, { where: { id: id } } //, userId: req.user.id
91+
}, { where: { id: id } }
9292
);
9393
res.redirect("/user_dashboard");
9494
} catch (err) {

middleware/auth.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,9 @@ const authenticate = (req, res, next) => {
2626
next(); // it goes to next action, controller
2727
});
2828
} catch (err) {
29-
console.log(err);
30-
return res.status(401).json({ success: false });
29+
// console.log(err);
30+
// return res.status(401).json({ success: false });
31+
res.send(`<script> document.cookie = "jwt_token=; max-age=-60"; window.location.href='/';</script>`);
3132
}
3233
};
3334

middleware/secure.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
const jwt = require("jsonwebtoken");
2+
3+
const secure = (req, res, next) => {
4+
try {
5+
// If user is already signed_in so, we want they not open the sign in page and supose they sign in page by any chance then he cant login again
6+
const token = req.cookies.jwt_token;
7+
if (token) {
8+
const user = jwt.verify(token, process.env.SECRET_KEY);
9+
if (user) {
10+
res.redirect('/user_dashboard');
11+
}
12+
// if token is not verified then it goes to catch block
13+
}
14+
next();
15+
} catch (err) {
16+
// console.log(err);
17+
// return res.status(401).json({ success: false });
18+
res.send(`<script> document.cookie = "jwt_token=; max-age=-60"; window.location.href='/';</script>`);
19+
}
20+
};
21+
22+
module.exports = secure;

router/user.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
const express = require('express');
22
const router = express.Router();
33

4-
//here userAuthentication is a middleware function
4+
//here userAuthentication and secure is a middleware function
55
const userAuthentication = require("../middleware/auth");
6+
const secure = require("../middleware/secure");
67

78
const homeController = require('../controllers/home_controller');
89

910
const userController = require('../controllers/user_controller');
1011

11-
router.get('/', homeController.homePage);
12+
router.get('/', secure, homeController.homePage);
1213

13-
router.post('/signUp', userController.signUp);
14+
router.post('/signUp', userController.signUp); // secure - we have to use this but for this time leave it
1415

15-
router.post('/signIn', userController.signIn);
16+
router.post('/signIn', secure, userController.signIn);
1617

1718
router.get('/user_dashboard', userAuthentication, userController.userDashboard);
1819

0 commit comments

Comments
 (0)