因此,我的應用使用通過ldap服務器登錄。一旦通過身份驗證,我登錄或在我的數據庫上創建用戶,然後繼續使用我的應用程序。如何在使用護照登錄時顯示閃存消息ldapauth
但是,我試圖顯示一個Flash消息,當用戶無法登錄ldap(無效的憑據或其他),但我不能這樣做的文檔說。
我用機車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,這應該更精確。
謝謝。我看到了消息的定製,但我不知道該放在'this.req.flash()'中。我找不到任何關於自定義回調的關鍵字,這不是我正在尋找的,更不用說'passport-ldapauth',我試過'failureFlash'和'message'但不是'錯誤',因爲鍵。現在這正是我所需要的,因爲我可以根據失敗的方式來執行錯誤消息。 –