-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreqauth.js
More file actions
29 lines (26 loc) · 1.1 KB
/
Copy pathreqauth.js
File metadata and controls
29 lines (26 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const jwt=require("jsonwebtoken");
const User=require( "../models/user");
const JWT_SECRET=process.env.JWT_SECRET;
async function isLoggedIn(req, res, next) {
const token = req.cookies?.token;
// console.log("**************************************");
// console.log("inside isLogged In");
// console.log("value of token is :" , token);
if (!token) return next(); // No token = not logged in
try {
const decoded = jwt.verify(token, JWT_SECRET);
const user = await User.findById(decoded._id);
// console.log(decoded);
// console.log("user is" , user);
if (!user) return next();
//console.log("we found the user with jwt as verified isLoggedIn");
// User is logged in — redirect to appropriate dashboard
if (user.role === "admin") return res.redirect("/admin");
if (user.role === "superadmin") return res.redirect("/superadmin");
return res.redirect("/user"); // normal user
} catch (err) {
//console.log(" error : Token invalid or expired isloggedin");
return next(); // Invalid token = treat as logged out
}
}
module.exports=isLoggedIn;