在/ login时验证失败,passport.js使用failureFlash保存错误消息并重定向到/ login(同一页面)。发送的HTML是正确的(即包括failureFlash消息),但在客户端被错误地呈现为“未定义”,'尽管(在Jade模板中)处于逻辑中以保证它的定义。我的HTML(GET /登录)和提交(POST /登录)的路由使用相同的URL;这可能是问题的一部分吗?整个代码库可以找到here。
app.js本地策略
passport.use(new LocalStrategy(
function(username, password, done) {
// asynchronous verification, for effect...
process.nextTick(function () {
var user = {'id': 1};
if(username !== "asdf") {
return done(null, false, { error: 'Unknown user '+username+'.'});
}else if(password !== "zxcv") {
return done(null, false, { error: 'Invalid password.'})
}
return done(null, user);
});
}
));
app.js登录获取路线
app.get('/login', function(req, res) {
var m1 = req.flash('error')[0]
console.log(m1)
res.render('login', { user: req.user, message: String(m1), sample: "Sanity check."});
});
app.js登录POST路线
app.post('/login',
passport.authenticate('local', { failureRedirect: '/login', failureFlash: 'THIS IS A SANITY CHECK ERROR', successFlash: 'Welcome!' }),
function(req, res) {
console.log('User login successful.');
var redirect_to = req.session.redirect_to || '/'
delete req.session.redirect_to;
res.send({redirect: String(redirect_to)})
//res.redirect(200, redirect_to);
});
app.get('/logout', function(req, res){
req.logout();
res.redirect(200, '/');
});
client_login.js随/ login的HTML
一起提供$(document).on('pageinit', function() {
$(document).on('click', '#submit', function() {
if($('#username').val().length > 0 && $('#password').val().length > 0){
// Send data to server through the ajax call
// action is functionality we want to call and outputJSON is our data
$.ajax({
url: '/login',
data: {action : 'login', username : $('#username').val(), password : $('#password').val()},
type: 'post',
async: 'true',
dataType: 'json'
})
.done(function (result) {
console.log('Done!');
window.location.replace(result.redirect);
})
.always(function (result) {
console.log('Always!');
})
.fail(function (request,error) {
console.log('Fail!');
// This callback function will trigger on unsuccessful action
//alert('Network error has occurred please try again!');
})
} else {
if($('#username').val().length <= 0 && $('#password').val().length > 0) {
alert('Please fill in your username.');
} else if($('#username').val().length > 0 && $('#password').val().length <= 0) {
alert('Please fill in your password.');
} else {
alert('Please fill in your username and password.');
}
}
return false; // cancel original event to prevent form submitting
})
})
login.jade
extends layout
block content
h1= title
if locals.expose.exists['client_login.js']
script(src='/dist/javascripts/client_login.js')
else
script(src='/javascripts/client_login.js')
div(style='max-width: 300px; margin:0 auto')
p(style='text-align: left') Please login to continue.
- if(typeof message !== 'undefined' && message.length > 0)
p Error! #{message}
p= sample
form(role='form', action='/login', method='post')
.form-group
input.form-control(type='text', data-clear-btn="true", name="username", id="username", value="", placeholder='Username')
.form-group
input.form-control(type='password', data-clear-btn="true", name="password", id="password", value="", placeholder='Password')
.form-group
button(type='submit', name='submit', id='submit' class="ui-btn ui-corner-all") Submit
从底部的服务器发送的HTML,在顶部呈现页面。注意&#34; undefined&#34; vs&#34;这是一个SANITY CHECK ERROR&#34;。另请注意,此项目使用jQuery Mobile。关于可能导致这种行为的任何想法?该页面的javascript不应该触摸它,但重定向也没有明显的闪烁,这很好,但让我怀疑。
答案 0 :(得分:0)
所以,我修改了AJAX帖子如下:
.fail(function (request,error) {
console.log('Fail!');
window.location.replace(window.location.href);
})
window.location.replace(window.location.href);
感觉有点笨拙&amp;可能有更好的方法来处理页面刷新,但目前,这是有效的。
...这就是我在这里发帖的原因。橡皮鸭调试工作。