-
Notifications
You must be signed in to change notification settings - Fork 0
/
JWTAuth.js
33 lines (28 loc) · 875 Bytes
/
JWTAuth.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
//config of jwt intercepter midware
const {expressjwt : expressJWT} =require('express-jwt');
const constant=require('./Constant')
const jwtConfig=expressJWT({
secret:constant.TOKEN_SECRET,
algorithms:["HS256"],
getToken: function fromHeaderOrQuerystring(req) {
if (
req.headers.authorization &&
req.headers.authorization.split(" ")[0] === "Bearer"
) {
return req.headers.authorization.split(" ")[1];
} else if (req.query && req.query.token) {
return req.query.token;
}
return null;
},
}).unless({path:['/login','/user/login','/user/register','/register']});
function jwtError(err, req, res, next) {
if (err.name === "UnauthorizedError") {
res.status(401).send("invalid token...");
} else {
next(err);
}
}
module.exports={
jwtConfig,jwtError
}