everyauth icon indicating copy to clipboard operation
everyauth copied to clipboard

res.locals.user always undefined (express3 branch)

Open dluces opened this issue 12 years ago • 4 comments

After updating to Express3 and using everyauth's express3 branch, the user variable in my templates was always undefined. I noticed that the middleware function to assign req.user to res.locals.user was being executed before the middleware that actually retrieved the user from the session and assigned it to req.user. Therefore, by the time it did res.locals.user = req.user, req.user would always be undefined.

So a fix to that was to re-add the middleware that assigns res.locals.user AFTER using everyauth middleware:

//Init calls..
app = express()
//Use everyauth
app.use(everyauth.middleware(app));
//And the fix:
app.use( function (req, res, next) {
    res.locals[everyauth.expressHelperUserAlias || 'user'] = req.user;
    next();
});

Am I the only one getting this?

dluces avatar Aug 16 '12 23:08 dluces

Same here, +1

behrendtio avatar Aug 20 '12 14:08 behrendtio

Same thing here... Oddly helpers like everyauth.loggedIn work fine.

I had to modify the fix above to this to work. I'm a total newbie so maybe I'm missing the obvious above, but this worked for knuckle draggers like me:

app.use( function (req, res, next) {
    res.locals.everyauth.user = req.user;
    res.locals.user = req.user;
    next();
});

pgray007 avatar Aug 29 '12 18:08 pgray007

the same. I think a merge from master can fix the problem?

jinze avatar Sep 25 '12 03:09 jinze

res.locas({user:req.user,everyauth:{user:req.user}});

guotingchao avatar Jul 25 '13 03:07 guotingchao