-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauth.js
More file actions
35 lines (33 loc) · 1.1 KB
/
Copy pathauth.js
File metadata and controls
35 lines (33 loc) · 1.1 KB
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
const jwtAuth = require("./jwtAuth")
const verifyAuthentication = (req, res, next) => {
// console.log("VERIFYING ------------ ", req.headers , " " )
if(req.method == "OPTIONS")return res.status(200);
if (req.headers && req.headers.authorization) {
let decoded;
try {
if(req.headers.authorization.split(" ")[0] !== "JWT")
res.status(401).send("Unauthorized Access");
const token = req.headers.authorization.split(" ")[1];
decoded = jwtAuth.verify(token);
} catch (e) {
return res.status(401).send('Unauthorized Access');
}
req.user = decoded;
next();
} else {
return res.status(401).send('Unauthorized Access');
}
}
const createTokens = (user) => {
const accessToken = jwtAuth.sign(user,{expiresIn:36000});
const refreshToken = jwtAuth.sign(user,{expiresIn:360000});
return {
accessToken,
refreshToken
}
}
const auth = {
verifyAuthentication,
createTokens
};
module.exports = auth;