authJwt.js Authentication JWT Node.js
const jwt = require("jsonwebtoken"); | |
const config = require("../config/auth.config.js"); | |
const db = require("../models"); | |
const User = db.user; | |
verifyToken = (req, res, next) => { | |
let token = req.headers["x-access-token"]; | |
if (!token) { | |
return res.status(403).send({ | |
message: "No token provided!" | |
}); | |
} | |
jwt.verify(token, config.secret, (err, decoded) => { | |
if (err) { | |
return res.status(401).send({ | |
message: "Unauthorized!" | |
}); | |
} | |
req.userId = decoded.id; | |
next(); | |
}); | |
}; | |
const authJwt = { | |
verifyToken: verifyToken | |
}; | |
module.exports = authJwt; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment