12345678910111213141516171819202122232425262728293031 |
- const jwt = require('jsonwebtoken')
- module.exports = function ( req, res, next ) {
- let token = req.headers['x-access-token'] || req.headers['authorization']
- let checkBearer = 'Bearer '
-
- if (token) {
-
- if (token.startsWith(checkBearer)) {
- token = token.slice(checkBearer.length, token.length)
- }
- jwt.verify(token, process.env.SECRET, (err, decoded) => {
- if (err) {
- res.json({
- success: false,
- message: 'Failed to authenticate'
- })
- } else {
- req.decoded = decoded
- next()
- }
- })
- } else {
- res.json({
- success: false,
- message: 'No token provided'
- })
- }
- }
|