-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
29 lines (24 loc) · 851 Bytes
/
auth.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
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) {
console.log("Invalid request. No authorization.")
response.status(401).json({
error: new Error("Invalid request. No authorization.")
})
}
}