I'm developing a node server for user authentication with Passport.js and its Local Strategy.
About the /login
part, here is my code :
router.get('/login', (req, res, next) => {
passport.authenticate('local', (err, user, info) => {
if(err) return next(err);
if(!user) return res.send('User not found');
req.logIn(user, (err) => {
if(err) return next(err);
console.log("coucou6");
return res.send(user.email);
})
})(req, res, next)
});
But it doesn't work. When I log err, it shows me a user object :
[nodemon] starting `node app.js`
Server on port 9000
Connected to Pitch Masters database
{ _id: 5d70df248aac0e462fd55b83,
email: 'coucou2@gmail.com',
password:
'$2a$10$w0PJny06QF6zINMXrBFiKuK5YWWxBneDqCAAa3pHzofFERXIiCm0S',
__v: 0 }
And when I log user and info, I get undefined
.
So, naively, I tried to reverse err
and user
, but then I get :
Error: passport.initialize() middleware not in use
at IncomingMessage.req.login.req.logIn (/Users/gabschemoul/Code/Professional/pitchmasters_webapp/server/node_modules/passport/lib/http/request.js:46:34)
at passport.authenticate (/Users/gabschemoul/Code/Professional/pitchmasters_webapp/server/routes/auth.js:62:9)
at Strategy.strategy.error (/Users/gabschemoul/Code/Professional/pitchmasters_webapp/server/node_modules/passport/lib/middleware/authenticate.js:353:18)
at verified (/Users/gabschemoul/Code/Professional/pitchmasters_webapp/server/node_modules/passport-local/lib/strategy.js:81:28)
at User.findOne.then (/Users/gabschemoul/Code/Professional/pitchmasters_webapp/server/config/passport.js:14:24)
at process._tickCallback (internal/process/next_tick.js:68:7)
Do you manage to guess what could go wrong ? Do I need to share more code with you ?
Thanks a lot !