-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathauthenticate.js
34 lines (28 loc) · 970 Bytes
/
authenticate.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
const jwt = require('jsonwebtoken');
exports.verifyUser = (req, res, next) => {
const { authorization } = req.headers;
if (!authorization) {
res.statusCode = 401;
res.setHeader('Content-Type', 'application/json');
res.json({err: "You are not authorized to view this content"});
return;
}
const token = authorization.replace("Bearer ", "");
jwt.verify(token, process.env.JWT_KEY, (err, payload) => {
if (err) {
res.statusCode = 401;
res.setHeader('Content-Type', 'application/json');
res.json({err: "Invalid Session"});
return;
}
const { _id } = payload;
if (_id !== process.env.PASSWORD) {
res.statusCode = 401;
res.setHeader('Content-Type', 'application/json');
res.json({err: "Invalid User"});
return;
}
req.isUserVerified = true;
next();
});
}