-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
25 lines (19 loc) · 688 Bytes
/
Copy pathauth.js
File metadata and controls
25 lines (19 loc) · 688 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
const jwt = require("jsonwebtoken");
module.exports = async (request, response, next) => {
try {
// get the token from the authorization header
const token = await request.headers.authorization.split(" ")[1];
//check if the token matches the supposed origin
const decodedToken = await jwt.verify(token, "RANDOM-TOKEN");
// retrieve the user details of the logged in user
const user = await decodedToken;
// pass the the user down to the endpoints here
request.user = user;
// pass down functionality to the endpoint
next();
} catch (error) {
response.status(401).json({
error: new Error("Invalid request!"),
});
}
};