したがって、私のアプリは LDAP サーバーを介したログインを使用します。認証されたら、データベースにログインするか、ユーザーを作成してから、アプリを続行します。
ただし、ユーザーが LDAP (無効な資格情報など) でのログインに失敗したときにフラッシュ メッセージを表示しようとしていますが、ドキュメントに記載されている方法では実行できません。
私は機関車の js をフレームワークとして使用しているので、このルーティングを引き続き使用したいと思います。
ルート.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>
<% } %>
パスポート.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 ログインが失敗した場合、verify コールバックが呼び出されないため、ログインの失敗を表示できないことです。それ以外の場合、フラッシュは他のいくつかのページで機能するため、機能します。このページでも機能しますが (たとえば、ユーザーが接続せずにページにアクセスしようとした場合)、ログインの失敗だけでは機能しません。他にもいろいろ試しましたが、これだけは確かです。
エラーを表示するために「/again」ルートを導入できますが、より正確な LDAP ルートが必要です。