-
Notifications
You must be signed in to change notification settings - Fork 0
/
auth.js
32 lines (26 loc) · 784 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
30
31
32
const config = require('./env')
const jwt = require ('jsonwebtoken')
// const secretKey = process.env.SECRET_KEY
const secretKey = '12345678'
const validateToken = async (request, reply) => {
try {
const { authorization } = request.headers
if (!authorization) {
throw new Error('missing authorization in headers')
}
const token = authorization.split(' ')[1]
console.log('token->', jwt.decode(token))
// token-> {
// id: '60f14345ec6685248ed03d81',
// iat: 1626453657,
// exp: 1626453777
// }
await jwt.verify(token, secretKey)
} catch (error) {
reply.statusCode = 401
throw error
}
}
module.exports = {
validateToken
}