Giriş sayfası yok, ancak her sayfada görünen bir giriş formum var. Ben (uygun flaş mesajlarla) bakılmaksızın kimlik doğrulama başarılı olup olmadığını ait olduğumuzu aynı sayfadaNodejs ve PassportJs: kimlik doğrulama başarısız olursa, passport.authenticate çağrıldığında middleware yeniden yönlendirin
geri kullanıcıyı yönlendirmek istiyorum aşağıdaki kodu atın:
app.post('/login', validateLogin, passport.authenticate('local-login'), function(req, res) {
var redirectUrl = '/';
if(req.body.to.length > 0){
redirectUrl = req.body.to;
}
console.log("THIS IS ONLY CALLED IF passport.authenticate() IS SUCCESSFUL");
res.redirect(redirectUrl);
});
Ben sadece çağrıldığını yukarıda nihai ortakatmanını bakın kimlik doğrulaması geçilirse. Başarısız olursa pasaport, bana bir istek isteği şeklinde giriş/çıkış yapıyormuş gibi görünmektedir. Uygulamamda bu sayfa mevcut değil.
Bir ek seçenekler geçerseniz
o zaman bu işleri pasaport kimlik doğrulaması fonksiyonunda parametre olarak itiraz:app.post('/login', validateLogin, passport.authenticate('local-login', {
successRedirect : '/', // redirect to the secure profile section
failureRedirect : '/signup', // redirect back to the signup page. THIS IS JUST FOR TESTING TO SEE IF THE REDIRECT ON FAIL WORKS.
failureFlash : true, // allow flash messages
}
));
Ama bunu yaparken de ben yeniden yönlendirmek için yeri seçmek için yeteneklerini kaybederler. Pasaport, kimlik doğrulama başarısız olursa kullanıcının yönlendirildiği yerde kontrolü ele geçiriyor gibi görünüyor. Bunu nasıl düzeltebilirim? Yoksa bu bir hata mı? Kimlik doğrulaması başarısız olursa pasaport kimlik doğrulaması zincirdeki son ara katman olmalı mı?
Bu benim yerel strateji fonksiyon çağrısıdır: orada http://passportjs.org/guide/authenticate/ son paragrafta açıklandığı gibi
//LOCAL LOGIN
passport.use('local-login', new LocalStrategy({
// by default, local strategy uses username and password, we will override with email
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true // allows us to pass back the entire request to the callback
},
function(req, email, password, done) { // callback with email and password from our form
console.log("IN PASSPORT");
if(email.length == 0 || password.length == 0){
console.log("FIELDS ARE EMPTY");
return done(null, false, req.flash('loginMessage', 'Fill in all values.'));
}
// find a user whose email is the same as the forms email
// we are checking to see if the user trying to login already exists
User.findOne({ 'local.email' : email }, function(err, user) {
// if there are any errors, return the error before anything else
if (err){
return done(err);
console.log("db err");
}
// if no user is found, return the message
if (!user){
console.log("not user");
return done(null, false, req.flash('loginMessage', 'Incorrect details.')); // req.flash is the way to set flashdata using connect-flash
}
// if the user is found but the password is wrong
if (!user.validPassword(password)){
console.log("invalid pw");
return done(null, false, req.flash('loginMessage', 'Incorrect details.')); // create the loginMessage and save it to session as flashdata
}
// all is well, return successful user
console.log("All OK");
return done(null, user);
});
}));
Bu çalışır. Teşekkürler. (Kendi kendine not edin ... her zaman kılavuzu okuyun). – Paulie
Savunmanızda, büyük bir konsept için oldukça ters bir el kitabıdır. (benzer sorunlarla karşılaşıyorum kendim ...) –
Bunun için 'yerel strateji' eklememiz gerekiyor mu? Benim durumumda, bana yerel strateji bilinmeyen bir hata veriyor. – adi