node.js - 如何在使用 Passport ldapauth 登录时显示闪现消息

标签 node.js passport.js locomotivejs

因此,我的应用程序通过 LDAP 服务器使用登录。经过身份验证后,我登录或在数据库上创建用户,然后继续使用我的应用程序。

但是,当用户无法使用 ldap 登录(无效凭据或其他原因)时,我尝试显示一条闪现消息,但我无法按照文档所述的方式进行操作。

我使用locomotive js作为框架,所以我想继续使用这个路由:

routes.js

this.match('/', 'pages#homepage',{ via: 'GET' });   

this.match('login', passport.authenticate('ldapauth', {
  successRedirect : '/dashboard', 
  failureRedirect : '/', 
  failureFlash : true 
})  ,{ via: 'POST' });

pagesController.js

pagesController.homepage = function() {
  this.title = 'title';
  this.render('',{messageRedirect: this.req.flash('messageRedirect')});
};

.ejs

<% if (messageRedirect.length>0) { %>
    <div class="alert alert-danger"><%= messageRedirect %></div>
<% } %> 

Passport .js

passport.use(new LdapStrategy( {

server: {
    url: config.ldap.url,
    bindDn:  config.ldap.bindDn,
    bindCredentials: config.ldap.bindCredentials,
    searchBase: config.ldap.searchBase,
    searchFilter: config.ldap.searchFilter,
    searchAttributes: config.ldap.searchAttributes,
    tlsOptions: {
        ca: [
            fs.readFileSync(config.ssl.cert)
        ],
        rejectUnauthorized: false
    }
}

}, function(ldapUser, done){

var queryUser = {...};

    user.getByUsername(queryUser, function(err,res){

        if (err) {
          return(err,null);
        }

        // if we don't find the user, it's his first attempt to login and we have to add him in the base
        if(res.length==0){

            user.create( queryUser, function(err,res){
                if (err){
                    return (err,null);
                }
            });

            var returnUser = {...};
            return done(null,returnUser,{
            message: 'Created and logged in Successfully'
            });

        }

        else{

            var returnUser = {...};
          return done(null, returnUser, {
            message: 'Logged In Successfully'
          });
        }
    });
   }
));

我遇到的问题是,如果 ldap 登录失败,则不会调用验证回调,因此我无法通过它显示登录失败。 否则闪光灯可以工作,因为它可以在其他几个页面上工作。它甚至可以在此页面上运行(例如,当用户尝试在未连接的情况下访问页面时),但不能用于登录失败。 我尝试了很多其他的东西,但这是我唯一确定的部分。

我可以引入“/again”路由来显示错误,但我想要 ldap 路由,这样应该更精确。

最佳答案

passport-ldapauth设置 LDAP 身份验证失败时的消息,并且 Passport.js then puts the message to req.flash如果未定义类型,则使用 error 作为键(passport-ldapauth 就是这种情况)。

您可以customize the messages当调用passport.authenticate时。如果使用 Microsoft AD,您可以 customize some additional messagesreadme 中所述

因此,要获取失败消息,这应该可行:

this.render('', {
  messageRedirect : this.req.flash('messageRedirect'),
  messageLdapError : this.req.flash('error')
});

关于node.js - 如何在使用 Passport ldapauth 登录时显示闪现消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38721465/

相关文章:

javascript - 如何扩展 GraphQL 调用的上下文对象?

javascript - 如何重命名Webpack中每个入口点的output.library选项?

node.js - 使用 Passport 进行 Facebook 身份验证的 Node+React 应用程序中的 CORS 错误

node.js - 路由上记录请求方法

javascript - req.user 未定义,但登录工作正常

node.js - LocomotiveJS 服务器端口更改

javascript - 如何在 Locomotive Js 中设置 CoffeeScript?

node.js - 在 nginx 后面使用expressjs设置响应头

node.js - 隐蔽 Node.js 加密 HMAC 到 Go-lang HMAC 加密

node.js - AWS lambda 层调用说 opt/ffmpeg 不是目录