Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const jwt = require("jsonwebtoken");
const User = require('../models/User');
module.exports = async function(req, res, next) {
const token = req.header("token");
if (!token) return res.status(401).json({ message: "Auth Error" });
try {
const decoded = jwt.verify(token, "randomString");
req.user = decoded.user;
const user = await User.findById(decoded.user.id);
next();
} catch (e) {
console.error(e);
res.status(500).send({ message: "Invalid Token" });
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment