quantdaddy
quantdaddy

Reputation: 1474

Passport authenticate options for Nodejs

I'm using passport for authentication in Nodejs like so:

const passport = require('passport');
passport.use(strategies.jwtLogin); //using require('passport-jwt')  
const requireAuth = passport.authenticate('jwt', {session: false});
app.get('/', requireAuth, (req, res, next) => next());

This works. My questions are:

  1. What are the different options for passport.authetication's first argument? I don't see any 'jwt' option in the documentation.

  2. How does this option affect the strategy I pass to the passport.use.

In other words, if I change 'jwt' option to something else like blah, how shall I change the strategy?

Upvotes: 3

Views: 4390

Answers (1)

AkinsTech
AkinsTech

Reputation: 164

The first argument to the authenticate() function is simply a short-hand name defined in the strategy to identify it. So if you were going to call authenticate('blah', ...) then you need to have a strategy defined in your passport.use() that has a defined name of "blah".

If you look at the source code for passport-jwt you'll see on line 31, that this shorthand name is defined.

this.name = 'jwt';

Likewise, if you look at another strategy like passport-local (line 53) you'll see it's shorthand name.

this.name = 'local';

It's setup this way to allow you to use multiple strategies in the same application. You could setup both jwt and local with their own passport.use() statements and then refer to each individually later in the code by using authenticate('local', ... ) or authenticate('jwt', ... ).

It could be better documented in the passport docs, but I hope that helps.

Upvotes: 1

Related Questions