How is req.isAuthenticated() in Passport JS implemented? [closed]

For any request you can check if a user is authenticated or not using this method. app.get(‘/some_path’,checkAuthentication,function(req,res){ //do something only if user is authenticated }); function checkAuthentication(req,res,next){ if(req.isAuthenticated()){ //req.isAuthenticated() will return true if user is logged in next(); } else{ res.redirect(“/login”); } }

How to authenticate Supertest requests with Passport?

As zeMirco points out, the underlying superagent module supports sessions, automatically maintaining cookies for you. However, it is possible to use the superagent.agent() functionality from supertest, through an undocumented feature. Simply use require(‘supertest’).agent(‘url’) instead of require(‘supertest’)(‘url’): var request = require(‘supertest’); var server = request.agent(‘http://localhost:3000’); describe(‘GET /api/getDir’, function(){ it(‘login’, loginUser()); it(‘uri that requires user to be … Read more

Use multiple local strategies in PassportJS

You can name your local strategies to separate them. // use two LocalStrategies, registered under user and sponsor names // add other strategies for more authentication flexibility passport.use(‘user-local’, new LocalStrategy({ usernameField: ’email’, passwordField: ‘password’ // this is the virtual field on the model }, function(email, password, done) { User.findOne({ email: email }, function(err, user) { … Read more

Using PassportJS, how does one pass additional form fields to the local authentication strategy?

There’s a passReqToCallback option that you can enable, like so: passport.use(new LocalStrategy( {usernameField: ’email’, passReqToCallback: true}, function(req, email, password, done) { // now you can check req.body.foo } )); When, set req becomes the first argument to the verify callback, and you can inspect it as you wish.

Why is PassportJS in Node not removing session on logout

Brice’s answer is great, but I still noticed an important distinction to make; the Passport guide suggests using .logout() (also aliased as .logOut()) as such: app.get(‘/logout’, function(req, res){ req.logout(); res.redirect(“https://stackoverflow.com/”); //Can fire before session is destroyed? }); But as mentioned above, this is unreliable. I found it behaved as expected when implementing Brice’s suggestion like … Read more

passport.js passport.initialize() middleware not in use

Follow the example to avoid the out-of-order middleware hell that express makes it so easy to enter. Straight from the docs. Note how yours does not match this exactly. var app = express(); app.use(require(‘serve-static’)(__dirname + ‘/../../public’)); app.use(require(‘cookie-parser’)()); app.use(require(‘body-parser’).urlencoded({ extended: true })); app.use(require(‘express-session’)({ secret: ‘keyboard cat’, resave: true, saveUninitialized: true })); app.use(passport.initialize()); app.use(passport.session()); Docs cookieParser session … Read more