-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathauth.js
More file actions
51 lines (42 loc) · 1.08 KB
/
Copy pathauth.js
File metadata and controls
51 lines (42 loc) · 1.08 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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const db = require('./db/models')
const loginUser = (req, res, user) => {
req.session.auth = {
userId: user.id,
};
};
const logoutUser = (req, res) => {
delete req.session.auth;
// res.redirect('/users/signup') // add this to check for delete feature.
}
const requireAuth = (req, res, next) => {
if (!res.locals.authenticated) {
return res.redirect('/users/login');
}
return next();
}
const restoreUser = async (req, res, next) => {
// console.log("SESSION:", req.session);
if (req.session.auth) {
const { userId } = req.session.auth;
try {
const user = await db.User.findByPk(userId);
if (user) {
res.locals.authenticated = true;
res.locals.user = user;
next()
}
} catch (err) {
res.locals.authenticated = false;
next(err)
}
} else {
res.locals.authenticated = false;
next();
}
}
module.exports = {
loginUser,
logoutUser,
requireAuth,
restoreUser
}