使用 node-passport 进行两种不同的登录 [英] two different login with node-passport

查看:45
本文介绍了使用 node-passport 进行两种不同的登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个站点的相同应用程序,前台和后台.两个站点都需要不同的登录条件,因为后台需要将 is_admin 值设为true".

I have the same application for 2 sites, the front-site and the backoffice.Both sites require a different condition to login because the backoffice needs to have the is_admin value in "true".

这是我的登录方式

module.exports.verifyCredentials = function (username, password, done) {

    mongoose.model("User").findOne({password:password, username:username, is_admin:true}, function(err, user) {
        done(err, user);
    });

};

这是在我的 app.js

and this is in my app.js

passport.use(new passportLocal.Strategy(securityCtl.verifyCredentials));

app.post("/administracion/login", passport.authenticate('local'), securityCtl.doLogin);

doLogin 方法只是进行重定向.

The doLogin methods just makes a redirect.

如何将前台登录和后台登录发送到不同的方法?

how can I send the frontsite login and the backoffice login to different methods?

谢谢!

推荐答案

只需在每个策略中使用不同的名称,并在 passport.authenticate 方法中通过它们的名称来引用它们.现在您可以在每个策略中指定不同的方法(verifyFrontSiteCredentials 分别用于前台和 verifyBackOfficeCredentials 用于后台).像这样:

Just use different names in each strategy and refers to they by their names in the passport.authenticate method. Now you can specify different methods (verifyFrontSiteCredentials for front-site and verifyBackOfficeCredentials for backoffice respectively) in each strategy. Something like this:

app.js

// front-site strategy
passport.use('front-site', new passportLocal.Strategy(securityCtl.verifyFrontSiteCredentials));

app.post("/administracion/front-site/login", passport.authenticate('front-site'), securityCtl.doLogin);

// backoffice strategy
passport.use('backoffice', new passportLocal.Strategy(securityCtl.verifyBackOfficeCredentials));

app.post("/administracion/backoffice/login", passport.authenticate('backoffice'), securityCtl.doLogin);

这篇关于使用 node-passport 进行两种不同的登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆