-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
41 lines (38 loc) · 936 Bytes
/
Copy pathutils.js
File metadata and controls
41 lines (38 loc) · 936 Bytes
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
import jwt from 'jsonwebtoken';
export const generateToken = (user) => {
return jwt.sign(
{
_id: user._id,
name: user.name,
email: user.email,
isAdmin: user.isAdmin,
},
process.env.JWT_SECRET,
{
expiresIn: '30d',
}
);
};
export const isAuth = (req, res, next) => {
const authorization = req.headers.authorization;
if (authorization) {
const token = authorization.slice(7, authorization.length); // Bearer XXXXXX
jwt.verify(token, process.env.JWT_SECRET, (err, decode) => {
if (err) {
res.status(401).send({ message: 'Invalid Token' });
} else {
req.user = decode;
next();
}
});
} else {
res.status(401).send({ message: 'No Token' });
}
};
export const isAdmin = (req, res, next) => {
if (req.user && req.user.isAdmin) {
next();
} else {
res.status(401).send({ message: 'Invalid Admin Token' });
}
};