-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjwt.js
More file actions
36 lines (34 loc) · 1.09 KB
/
Copy pathjwt.js
File metadata and controls
36 lines (34 loc) · 1.09 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
36
const jwt = require('jsonwebtoken')
const jwtKey = 'exemple_cours_secret_key'
const jwtExpirySeconds = 86400
module.exports = (userAccountService) => {
return {
validateJWT(req, res, next) {
if (req.headers.authorization === undefined) {
res.status(401).end()
return
}
const token = req.headers.authorization.split(" ")[1];
jwt.verify(token, jwtKey, {algorithm: "HS256"}, async (err, user) => {
if (err) {
res.status(401).end()
return
}
console.log(user)
try {
req.user = await userAccountService.dao.getByLogin(user.login)
return next()
} catch(e) {
console.log(e)
res.status(401).end()
}
})
},
generateJWT(login) {
return jwt.sign({login}, jwtKey, {
algorithm: 'HS256',
expiresIn: jwtExpirySeconds
})
}
}
}