-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
32 lines (27 loc) · 868 Bytes
/
app.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
const jwt = require('jsonwebtoken');
function authGuard(jwt_secret_key) {
return (req, res, next) => {
try {
const authorization = req.headers.authorization;
if (authorization && authorization.length > 0) {
// get token
const token = authorization.split(' ')[1];
// verify token
const decode = jwt.verify(token, jwt_secret_key);
// get user info
req.user = decode;
next();
} else {
res.status(401).json({
error: 'Incorrect Auth Information!',
});
}
} catch (error) {
res.status(401).json({
error: 'Authetication failure!',
});
}
}
}
// export module
module.exports = authGuard;