Reputation: 105
I am trying to build an authentication section with NodeJS, Express and Mongoose.
I use passport, passport-local and passport-local-mongoose.
Initially, I thought it could be related to express-session so I tried replacing that one with cookie-session but no dice.
After authenticating a newly registered user and redirecting them to a dashboard, the dashboard does not render properly. It seems like the header I have as a partial is not included. Styles doesn't get applied and the script, which is included in the footer, generates an error on line 1 -> $ is not defined.
I have configured the app.js file with:
app.use(passport.initialize());
app.use(passport.session());
I have this in my user schema:
userSchema.plugin(passportLocalMongoose, {
usernameField : "email"
});
And I put this in my user route: (It also used to be in the app.js file but with the same result)
passport.use(User.createStrategy());
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
.
.
} else {
passport.authenticate("local")(req, res, function() {
res.redirect(req.user.email)
})
.
.
if I do res.redirect(req.user.email) without authenticating the user the page renders as it should so this tells me it is likely something with passport.authenticate() that is causing it.
Any suggestions would be appreciated. Let me know if I need to include more, but I tried to be as minimal as possible since there is a lot of stuff that is not relevant to this issue.
Upvotes: 0
Views: 106
Reputation: 105
Finally found what was causing this behaviour. I had set req.user to res.locals.currentUser and used that in the header section of the body. Not sure why this isn't working now, because I've had it working before... Maybe it had something to do with EJS, but either way, after stripping some unnecessary parts it is now working again.
Upvotes: 0