Hi Jared, i've been implementing a REST API and need to implement a login route.
I've read the docs and you example here, but I am receiving an error that I can't identify from where it comes from, the error is:
TypeError: req.flash is not a function at allFailed (/home/caio/Documents/Projetos-Outros/StartupOne/API/API-Pet/node_modules/passport/lib/middleware/authenticate.js:118:15) at attempt (/home/caio/Documents/Projetos-Outros/StartupOne/API/API-Pet/node_modules/passport/lib/middleware/authenticate.js:167:28) at Strategy.strategy.fail (/home/caio/Documents/Projetos-Outros/StartupOne/API/API-Pet/node_modules/passport/lib/middleware/authenticate.js:284:9) at Strategy.authenticate (/home/caio/Documents/Projetos-Outros/StartupOne/API/API-Pet/node_modules/passport-local/lib/strategy.js:75:17) at attempt (/home/caio/Documents/Projetos-Outros/StartupOne/API/API-Pet/node_modules/passport/lib/middleware/authenticate.js:348:16) at authenticate (/home/caio/Documents/Projetos-Outros/StartupOne/API/API-Pet/node_modules/passport/lib/middleware/authenticate.js:349:7) at Layer.handle [as handle_request] (/home/caio/Documents/Projetos-Outros/StartupOne/API/API-Pet/node_modules/express/lib/router/layer.js:95:5) at next (/home/caio/Documents/Projetos-Outros/StartupOne/API/API-Pet/node_modules/express/lib/router/route.js:137:13) at Route.dispatch (/home/caio/Documents/Projetos-Outros/StartupOne/API/API-Pet/node_modules/express/lib/router/route.js:112:3) at Layer.handle [as handle_request] (/home/caio/Documents/Projetos-Outros/StartupOne/API/API-Pet/node_modules/express/lib/router/layer.js:95:5)
My code is as follows:
//strategy config
passport.use( new localStrategy({
passReqToCallback : true
}, //Here I tried to follow an awnser from stackoverflow, setting req as a parameter, but didn't help me
function(req, username, password, done){
const validate = new ValidateLoginDAO();
//Verify callback
validate.isValidUser({username: username}, (err, user) => {
if(err){ return done(err) }
if(!user){ return done(null, false, req.flash('signupMessage','!user' )) }
if(user.senha !== password){ return done(null, false, req.flash('signupMessage','user.senha' )) }
return done(null, user);
});
}
));
//Middleware imlpementation without session
app.use(passport.initialize());
//POST route
router.post('/login',
passport.authenticate('local', {
session: false,
failureFlash: true
}),
(req, res) => {
console.log('autenticado ->', req.username);
res.json({validado: 'ok'})
});
Thanks a lot!
Hi Jared, i've been implementing a REST API and need to implement a login route.
I've read the docs and you example here, but I am receiving an error that I can't identify from where it comes from, the error is:
TypeError: req.flash is not a function at allFailed (/home/caio/Documents/Projetos-Outros/StartupOne/API/API-Pet/node_modules/passport/lib/middleware/authenticate.js:118:15) at attempt (/home/caio/Documents/Projetos-Outros/StartupOne/API/API-Pet/node_modules/passport/lib/middleware/authenticate.js:167:28) at Strategy.strategy.fail (/home/caio/Documents/Projetos-Outros/StartupOne/API/API-Pet/node_modules/passport/lib/middleware/authenticate.js:284:9) at Strategy.authenticate (/home/caio/Documents/Projetos-Outros/StartupOne/API/API-Pet/node_modules/passport-local/lib/strategy.js:75:17) at attempt (/home/caio/Documents/Projetos-Outros/StartupOne/API/API-Pet/node_modules/passport/lib/middleware/authenticate.js:348:16) at authenticate (/home/caio/Documents/Projetos-Outros/StartupOne/API/API-Pet/node_modules/passport/lib/middleware/authenticate.js:349:7) at Layer.handle [as handle_request] (/home/caio/Documents/Projetos-Outros/StartupOne/API/API-Pet/node_modules/express/lib/router/layer.js:95:5) at next (/home/caio/Documents/Projetos-Outros/StartupOne/API/API-Pet/node_modules/express/lib/router/route.js:137:13) at Route.dispatch (/home/caio/Documents/Projetos-Outros/StartupOne/API/API-Pet/node_modules/express/lib/router/route.js:112:3) at Layer.handle [as handle_request] (/home/caio/Documents/Projetos-Outros/StartupOne/API/API-Pet/node_modules/express/lib/router/layer.js:95:5)My code is as follows:
//strategy config
passport.use( new localStrategy({
passReqToCallback : true
}, //Here I tried to follow an awnser from stackoverflow, setting req as a parameter, but didn't help me
function(req, username, password, done){
const validate = new ValidateLoginDAO();
//Verify callback
validate.isValidUser({username: username}, (err, user) => {
if(err){ return done(err) }
}
));
//Middleware imlpementation without session
app.use(passport.initialize());
//POST route
router.post('/login',
passport.authenticate('local', {
session: false,
failureFlash: true
}),
(req, res) => {
console.log('autenticado ->', req.username);
res.json({validado: 'ok'})
});
Thanks a lot!