jaredhanson / passport-local

Username and password authentication strategy for Passport and Node.js.
https://www.passportjs.org/packages/passport-local/?utm_source=github&utm_medium=referral&utm_campaign=passport-local&utm_content=about
MIT License
2.74k stars 498 forks source link

add options.strategyName to allow multiple passport-local instances to work in one node.js app; #53

Closed caiguanhao closed 11 years ago

caiguanhao commented 11 years ago

Example code:

// login for normal users:

passport.use(new LocalStrategy({ strategyName: 'local' }, ... );

app.post('/login', passport.authenticate('local', {
  successReturnToOrRedirect: '/',
  failureRedirect: '/login'
}));

app.get('/', function(req, res, next){
  if (req.user) {
    res.render('index');
  } else {
    res.redirect('/login');
  }
});

// login for admins:

passport.use(new LocalStrategy({ strategyName: 'local-admin' }, ... );

app.post('/admin/login', passport.authenticate('local-admin', {
  successReturnToOrRedirect: '/admin',
  failureRedirect: '/admin/login'
}));

app.get('/admin', function(req, res, next){
  if (req.user && req.user.is_admin) {
    res.render('admin/index');
  } else {
    res.redirect('/admin/login');
  }
});
jaredhanson commented 11 years ago

You can set the strategy name when registering with Passport:

passport.use(new LocalStrategy( ... ));

passport.use('local-admin', new LocalStrategy( ... ));