-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.js
35 lines (33 loc) · 906 Bytes
/
utility.js
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
import jwt from 'jsonwebtoken';
export const generateToken = (user) => {
//jwt: jsonwebtoken
return jwt.sign({
_id: user._id,
name: user.name,
email: user.email,
Admin: user.Admin
}, process.env.JWT_SECRET || 'hiddentext', {
expiresIn: '30d',
});
}
//middleware to authenticate user
export const isAuth = (req, res, next) => {
const authorization = req.headers.authorization;
if (authorization) {
const token = authorization.slice(7, authorization.length);
jwt.verify(
token,
process.env.JWT_SECRET || 'hiddentext',
(err, decode) => {
if (err) {
res.status(401).send({ message: 'Invalid Token' });
} else {
req.user = decode;
next();
}
}
);
} else {
res.status(401).send({ message: 'No Token Exists' });
}
};