Tôi đang sử dụng passport.js và tôi muốn flash một thông báo nếu các trường trong biểu mẫu của tôi trống. Nhưng tôi không biết làm thế nào để làm điều đó kể từ khi hộ chiếu không kích hoạt các cuộc gọi lại chiến lược nếu những người đang mất tích. Tôi thực sự muốn trường hợp sử dụng này rõ ràng hơn và tôi không muốn sửa đổi hộ chiếu. Tôi cảm thấy như có một cách để làm như vậy nhưng tôi không biết ở đâu! Tôi đã cố gắng sử dụng gọi lại của các tuyến đường (app.post
) nhưng nó dường như không hoạt động theo cách tôi đã cố gắng.Xác thực Node.js với Hộ chiếu: Cách flash một tin nhắn nếu một trường bị thiếu?
Dưới đây là chức năng xác thực nguyên mẫu:
Strategy.prototype.authenticate = function(req, options) {
options = options || {};
var username = lookup(req.body, this._usernameField) || lookup(req.query, this._usernameField);
var password = lookup(req.body, this._passwordField) || lookup(req.query, this._passwordField);
// here is my problem
if (!username || !password) {
return this.fail({ message: options.badRequestMessage || 'Missing credentials' }, 400);
}
var self = this;
function verified(err, user, info) {
if (err) { return self.error(err); }
if (!user) { return self.fail(info); }
self.success(user, info);
}
try {
if (self._passReqToCallback) {
this._verify(req, username, password, verified);
} else {
this._verify(username, password, verified);
}
} catch (ex) {
return self.error(ex);
}
};
Đây là chiến lược của tôi:
passport.use('local-login', new LocalStrategy({
usernameField : 'email',
passwordField : 'password',
passReqToCallback : true
},
function(req, email, password, done) {
// ...
console.log("Hello");
User.findOne({ 'local.email' : email }, function(err, user) {
if (err)
return done(err);
// if no user is found, return the message
if (!user)
return done(null, false, req.flash('loginMessage', 'Pas d\'utilisateur avec ce login.')); // 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))
return done(null, false, req.flash('loginMessage', 'Oops! Mauvais password.')); // create the loginMessage and save it to session as flashdata
// all is well, return successful user
return done(null, user);
});
}));
Và cuối cùng con đường của tôi:
app.get('/login', function(req, res) {
// render the page and pass in any flash data if it exists
res.render('login', { title: "Connexion", message: req.flash('loginMessage') });
});
// process the login form
app.post('/login', passport.authenticate('local-login', {
successRedirect : '/profile', // redirect to the secure profile section
failureRedirect : '/login', // redirect back to the signup page if there is an error
failureFlash : true // allow flash messages
}, function(err, user, info) {
// Was trying this callback, does'nt work, post callback maybe ?
console.log("Hello");
}));
đâu là ngọc bích hoặc mẫu của bạn? bạn nên kiểm tra 'message', và hiển thị nếu nó hiện diện – anvarik
Chính xác: Có một thông báo flash đã có khi người dùng hoặc mật khẩu sai, nhưng không phải khi các trường trống. Và tôi không thể quản lý để làm điều đó kể từ khi gọi lại trong chiến lược không được gọi trong kịch bản này bằng hộ chiếu (xem nguyên mẫu xác thực). Tôi không muốn hack vào mã hộ chiếu, phải có một cách .. –
trả lại this.fail ({message: options.badRequestMessage || 'Thiếu thông tin đăng nhập'}, 400); <- điều này được trả về thay vào đó, và tôi không biết làm thế nào tôi có thể nhận được điều đó, ví dụ như trong hàm gọi lại app.post của tôi. –