【发布时间】:2019-05-19 13:28:47
【问题描述】:
我正在使用 Node.js(express) + firestore 实现登录控件。
我尝试使用搜索查询进行id 和密码(pw)检查。
如果用户输入数据(来自 HTML <form>)在 firestore DB 中,客户端页面将重定向到 /main。 else 将重定向回来。
当数据在 firestore 数据库中时它可以工作,但是当数据不在 firestore 数据库中时,它不适用于无限循环并且没有redirect。
我检查了数据(id,pw)并进行了测试。但是当数据不在firestore DB中时,它总是无限循环。
查看代码是这样的。
<form action="" method="post">
<input placeholder="ID" name="id" id="id" type="text">
<input placeholder="PASSWORD" name="pw" id="password" type="password">
<button type="submit">SIGN-IN</button>
</form>
<button onclick="location.href='/signup'">SIGN-UP</button>
而服务器中的代码是这样的
var users_info = db.collection('users')
.where('id', '==', req.body.id).where('pw', '==', sha256(req.body.pw))
.get().then(snapshot => {
snapshot.forEach(doc => {
if (doc.exists) {
req.session.user = {
'id': doc.data().id,
'name': doc.data().name,
'auth': doc.data().auth,
'is_guest': doc.data().is_guest
}
res.redirect('/main');
} else {
res.redirect('back');
}
});
})
.catch(err => {
console.log(err);
res.redirect('back');
});
【问题讨论】:
-
如果在没有匹配文档时发生循环,这意味着
res.redirect('back')没有按照您的预期执行。除非 back 对您的应用程序或您使用的框架有特殊含义,否则我怀疑这是将浏览器重定向到上一页的方式。见stackoverflow.com/questions/12442716/… 和npmjs.com/package/express-back
标签: node.js google-cloud-firestore