Skip to content

Instantly share code, notes, and snippets.

@blogcacanid
Created November 12, 2020 07:46
Show Gist options
  • Save blogcacanid/1c8aa39355ffa987ea5747c6515601e3 to your computer and use it in GitHub Desktop.
Save blogcacanid/1c8aa39355ffa987ea5747c6515601e3 to your computer and use it in GitHub Desktop.
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