Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

set authentication on two entity of user #182

Open
behzadnm99 opened this issue Jul 20, 2019 · 2 comments
Open

set authentication on two entity of user #182

behzadnm99 opened this issue Jul 20, 2019 · 2 comments

Comments

@behzadnm99
Copy link

behzadnm99 commented Jul 20, 2019

Environment

  • Node version: lts
  • passport version: latest
  • passport-local version: latest

hello guys. i using passport-local for authentication and i want apply local auth strategy on two type of user : normal users and admin.

how to handle this senario with passport-local?

please help me.
thank you.

@usamamashkoor
Copy link

@behzadnm99 did you find any solution for this? I am facing the same issue here.

@martiendt
Copy link

you can rename your strategy, for example :

passport.use("local-admin", new LocalStrategy(
  function(username, password, done) {
    UserAdmin.findOne({ username: username }, function (err, user) {
      if (err) { return done(err); }
      if (!user) { return done(null, false); }
      if (!user.verifyPassword(password)) { return done(null, false); }
      return done(null, user);
    });
  }
));
passport.use("local-user", new LocalStrategy(
  function(username, password, done) {
    User.findOne({ username: username }, function (err, user) {
      if (err) { return done(err); }
      if (!user) { return done(null, false); }
      if (!user.verifyPassword(password)) { return done(null, false); }
      return done(null, user);
    });
  }
));

and in your route :

app.post('/admin/login', 
  passport.authenticate('local-admin', { failureRedirect: '/admin/login' }),
  function(req, res) {
    res.redirect('/admin');
  });

app.post('/login', 
  passport.authenticate('local-user', { failureRedirect: '/login' }),
  function(req, res) {
    res.redirect('/');
  });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants