todos-express-password
todos-express-password copied to clipboard
Confusing TypeError req.flash is not a function
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!
Hello zscaiosi, Do you have a link to your project? I'm afraid this code isn't sufficient for me to help you. Examining your code, and guessing, I can point you in two possible directions. First is looking in the allFailed to see if you can get any information passed from the flash module. What I mean is: Remove all code, without including the flash module in this file, and see if you can get a response from flash. I can't see which modules you are requiring in your project, but if you're using the standard connect-flash (const flash=require('connect-flash'); make sure it is included in your main app.
What I think is the real problem here is that you're using the default name (local) whereas you should rename your custom logic with a custom name.
Try replacing
passport.use( new localStrategy({
with
passport.use('my-custom-name', new localStrategy({
And where it is appropriate.
Remember to include const localStrategy = require('passport-local').Strategy; at the top of your file.
Edit: You also need to add the fields for username and password.
passport.use('my-custom-name', new LocalStrategy({
// local strategy uses username and password. These settings are overriding the values.
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true // allows us to pass back the entire request to the callback
},
Read more at the official passport-local repo.