Wes*_*ton 5 javascript ajax jquery node.js flash-message
我使用护照和闪存来管理身份验证。我想根据文档将闪现消息发送回用户界面。在我的用户界面场景中,我使用的是模态,所以执行
res.render('login.ejs', { message: req.flash('loginMessage') });
Run Code Online (Sandbox Code Playgroud)
不起作用,因为您只能在页面刷新时呈现。因此,当由于某种原因无法登录时,如何将闪存数据或任何其他类型的数据发送到我的页面。无论特别是 Flash 数据如何,我都无法弄清楚如何使用 Express 将数据渲染到模态。
路线.js
低于 res.render 的情况永远不会在身份验证失败时发生。
//Home Page ===================================
app.get('/', function(req, res) {
res.render('login.ejs', { message: req.flash('loginMessage') });
});
//Login Modal =================================
app.post('/login', passport.authenticate('local-login', {
successRedirect : '/profile',
failureFlash : true
}));
Run Code Online (Sandbox Code Playgroud)
index.ejs (我的模式在哪里)
res.render('login.ejs', { message: req.flash('loginMessage') });
Run Code Online (Sandbox Code Playgroud)
我的理解是我需要使用 javascript/ajax 来防止帖子被重定向,但我无法弄清楚如何获取 Flash 数据:
索引.ejs (javascript)
//Home Page ===================================
app.get('/', function(req, res) {
res.render('login.ejs', { message: req.flash('loginMessage') });
});
//Login Modal =================================
app.post('/login', passport.authenticate('local-login', {
successRedirect : '/profile',
failureFlash : true
}));
Run Code Online (Sandbox Code Playgroud)
编辑从 Passport.js 添加了一些代码
function passport_login(req, email, password, done) {
//doing some validate and found bad password
return done(null, false, req.flash('loginMessage', 'Invalid Password'));
})
Run Code Online (Sandbox Code Playgroud)
因此,经过大量挖掘后,我找到了解决方案。关键是要认识到 jquery 而不是express才是答案,让客户端处理重定向和posts/gets。我只包含本地登录策略的更新,但它们也适用于您的注册/社交其他社交资料。
对 Passport.js 的更改(较小)
function passport_login(req, email, password, done) {
//doing some validate and found bad password
return done(null, false, 'Bad Password');
})
Run Code Online (Sandbox Code Playgroud)
路线的改变(我遇到困难的部分)
这里需要注意的关键是我正在决定通过 res.send 发送哪些数据。我可以发送任何内容,在本例中,信息来自 Passport.js(“错误密码”)。如果我很高兴并且能够登录,我会发送一个简单的小 valid:true json。
//Login Modal =================================
app.get('/localLogin', function(req, res, next) {
passport.authenticate('local-login', function(err, user, info) {
if (err) { return next(err); }
//if there is no user in the response send the info back to modal
if (!user) {
return res.send(info);
}
//user was able to login, send true and redirect
req.logIn(user, function(err) {
if (err) { return next(err); }
return res.send({ valid: true });
});
})(req, res, next);
});
Run Code Online (Sandbox Code Playgroud)
对我的 index.js 的更改
在我的模态顶部添加一个 div 来显示消息,删除旧的 <%= message > 部分并从表单中删除操作和方法,jQuery 将为我们做到这一点。(包括完整的模式代码以保持透明)
function passport_login(req, email, password, done) {
//doing some validate and found bad password
return done(null, false, 'Bad Password');
})
Run Code Online (Sandbox Code Playgroud)
然后我添加了以下 Jquery 代码作为拼图的最后一块:
//Login Modal =================================
app.get('/localLogin', function(req, res, next) {
passport.authenticate('local-login', function(err, user, info) {
if (err) { return next(err); }
//if there is no user in the response send the info back to modal
if (!user) {
return res.send(info);
}
//user was able to login, send true and redirect
req.logIn(user, function(err) {
if (err) { return next(err); }
return res.send({ valid: true });
});
})(req, res, next);
});
Run Code Online (Sandbox Code Playgroud)
希望这对某人有帮助,有关如何执行此操作的完整端到端解决方案的信息很少。
归档时间: |
|
查看次数: |
6100 次 |
最近记录: |