-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathauthJwt.js
More file actions
34 lines (28 loc) · 1006 Bytes
/
authJwt.js
File metadata and controls
34 lines (28 loc) · 1006 Bytes
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
const jwt = require('jsonwebtoken')
const config = require('./config.js')
verifyToken = (req, res, next) => {
let token = req.headers['authorization']
//Check is token is available, stop request if the token is not available
if (!token) {
return res.status(200).send({
error: true,
msg: 'Please login to continue using the service.'
})
}
// Verify if token sent was created using the config secret
jwt.verify(token, config.secret, (err, decoded) => {
//Check is token is valid, stop request if the token is not invalid
if (err) {
console.log(err)
return res.status(200).send({
error: true,
msg: 'Authentication Failed.'
})
}
req.key = decoded.key // Get the unecrpyted key and assign to a new request variable
next() // Continue with request
})
}
const authJwt = {}
authJwt.verifyToken = verifyToken
module.exports = authJwt