Created
November 12, 2020 07:46
-
-
Save blogcacanid/1c8aa39355ffa987ea5747c6515601e3 to your computer and use it in GitHub Desktop.
authJwt.js Authentication JWT Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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