问题描述
const jwt = require('jsonwebtoken');
const { User } = require('../model/user');
const authenticate = async (req, res, next) => {
try {
const token = req.cookies.jwtoken
console.log(`i am from authorization `)//This line never execute
if (!token) return res.status(403).json({ message: "Access denied" })
//all user info from database will be saved of this verified token
const decoded = jwt.verify(token, process.env.SECRET_KEY);
const rootUser = await User.findOne({ _id: decoded._id, 'tokens.token': token })
if (!rootUser) return res.status(400).send("User Not Found")
req.token = token;
req.rootUser = rootUser;
req.userId = rootUser._id
next();
} catch (err) {
res.status(401).send("unathorized!No token Provided")
next()
}
}
module.exports = authenticate;
标签: node.jsmongodbcookiesauthorization
解决方案
推荐阅读