-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.js
More file actions
31 lines (31 loc) · 856 Bytes
/
utils.js
File metadata and controls
31 lines (31 loc) · 856 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
const jwt = require("jsonwebtoken");
module.exports = {
validateToken: (req, res, next) => {
try {
const authorizationHeaader = req.headers.authorization;
let result;
if (authorizationHeaader) {
const token = req.headers.authorization.split(" ")[1]; // Bearer <token>
const options = {
expiresIn: "7d",
issuer: "https://api.tarunsingh.dev",
};
result = jwt.verify(token, process.env.JWT_SECRET, options);
req.decoded = result;
next();
} else {
result = {
error: `Authentication error. Token required.`,
status: 401,
};
res.status(401).send(result);
}
} catch (err) {
result = {
error: `Authentication error.`,
status: 401,
};
res.status(401).send(result);
}
},
};