Git Product home page Git Product logo

Comments (4)

jaredhanson avatar jaredhanson commented on May 18, 2024 1

Yes, that example has been confusing, since it didn't include the full scope. I've updated the documentation to clarify. Here's what it should look like:

app.get('/protected', function(req, res, next) {
  passport.authenticate('local', function(err, user, profile) {
    if (err) { return next(err) }
    if (!user) { return res.redirect('/signin') }
    res.redirect('/account');
  })(req, res, next);
});

When using the callback function, call authenticate() within your route handler, as opposed to using route middleware. That way the callback has access to req and res via the closure.

That should fix your issue. Let me know if you run into any others.

from passport.

jaredhanson avatar jaredhanson commented on May 18, 2024 1

@oroce For custom callbacks, that is the expected behavior.

When using custom callbacks, Passport defers handling to the application, and simply supplies the callback with the authenticated user and associated information. It won't modify the request, response, or session. If the app wants to establish a login session, it needs to call req.logIn(...) manually, as your example does.

As reported in #10, this isn't documented clearly. I'm in the process of updating the docs.

from passport.

oroce avatar oroce commented on May 18, 2024

As I can see, with callback req.logIn hasn't been called ever.

I'm using the following code: (it's coffeescript):

checkPassword: ( req, res, next ) ->
    passport.authenticate( "local", ( err, user, profile ) ->

        if err then return next( err )
        if !user 
            req.flash(
                "login-error",
                "User not exists."
            )
            return res.redirect( "/login" )
        req.logIn(user, {}, (err) ->
            if (err)
                return next(err)

            next();
        )
    )(req, res, next)

from passport.

oroce avatar oroce commented on May 18, 2024

@jaredhanson I see.

I posted my code to show the solution, if somebody wants a custom hack, like me:)

from passport.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.