Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
const jwt = require('jsonwebtoken');
const User = require('../Model/User');
const cookieParser = require('cookie-parser');
const auth = async (req, res, next) => {
try{
const token = req.cookies.token;
const decoded = jwt.verify(token, 'S65jxokbIei42ObklX3Z');
const user = await User.findOne({_id: decoded._id, 'tokens.token':token});
if(!user){
throw new Error();
}
req.token = token;
req.user = user;
next();
}catch (e) {
res.redirect('/login');
}
}
module.exports = auth;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment