Comments (4)
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.
@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.
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.
@jaredhanson I see.
I posted my code to show the solution, if somebody wants a custom hack, like me:)
from passport.
Related Issues (20)
- strategy.authenticate is not a function (passport-facebook)
- documentation does not work. https://www.passportjs.org/tutorials/google/ HOT 3
- Race condition in logout function HOT 10
- Laravel passport HOT 2
- TypeError: Cannot set property 'user' of undefined After passport upgradation from 0.4.1 to 0.6.0 in Nestjs 7. HOT 2
- https://www.passportjs.org/packages/ this page not found HOT 3
- HTTP Strategy abandoned and broken HOT 1
- [Website] Add Keycloak and FusionAuth bearer strategies to website HOT 1
- The authentication middleware deletes the data that were inside the session before authentication, verssion 0.6.0 HOT 3
- TokenError: Code is invalid or expired HOT 2
- TypeError: LocalStrategy is not a constructor HOT 1
- Own session data is overwritten by passport HOT 5
- Facebook authentication no more working HOT 2
- passport-google-oauth20 passport.authenticate doesn't run HOT 7
- social login with yahoo using passprt js HOT 1
- https://www.passportjs.org/packages/ not found
- Passport Strategy for Facebook doesn't work
- local strategy deserializeUser not be used
- Add Discord Auth Strategy
- Add my Passport Strategies to website: Magalu (Magazine Luiza) and MercadoLivre (ML Brazil)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
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.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from passport.