Need help with passing user object to res.locals from a middleware

So i am trying to pass user data as an object to res.locals.user from a middleware that checks if user is logged in and gives logged in user data and i want to display that data in components of the view templates like in the header or user’s dashboard but the view doesn’t seem to read that data. Says user is not defined
This is the middleware below;

exports.currentUser = function(req, res, next) {
	const token = req.cookies.jwt;
	if (token) {
		jwt.verify(token, secret, async(err, decodedToken)=> {
			if (err) {
				res.locals.user = null;
				//log(res.locals.user);
				next();
			}
			else {
				let user = await Agent.findById(decodedToken.id);
				res.locals.user = user;
				//log(res.locals.user);
				next();		
			}
		});
	}
	else res.locals.user = null;
	next();
}

What can i be doing wrong?

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.