Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Confusing TypeError req.flash is not a function #8

Open
zscaiosi opened this issue Jul 23, 2017 · 1 comment
Open

Confusing TypeError req.flash is not a function #8

zscaiosi opened this issue Jul 23, 2017 · 1 comment
Labels
question Request for help or support

Comments

@zscaiosi
Copy link

zscaiosi commented Jul 23, 2017

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!

@Megahard-Doors
Copy link

Megahard-Doors commented Jul 30, 2017

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.

@jaredhanson jaredhanson added the question Request for help or support label Jan 14, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Request for help or support
Projects
None yet
Development

No branches or pull requests

3 participants