【发布时间】:2018-04-15 08:38:36
【问题描述】:
我只是陷入了一个问题,也不知道问题是否正确。
我正在使用 ejs 模板引擎开发 node express js。
我已经创建了一些通用端点(比如 login(POST) 端点)。
现在我想创建一个新的应用程序,它应该使用这些端点并根据这些端点的结果呈现视图。
假设我有一个通用的登录端点
router.post('/user/login', function (req, res) {
var userLoginId = req.body.username;
var currentPassword = req.body.password;
UserLogin.findOne({userLoginId: userLoginId, currentPassword: currentPassword}, function (err, usr) {
if (err) {
console.log('Error in User login');
console.log(err);
res.status(500).type('application/problem+json').send();
} else {
if (!usr) {
console.log('Please enter valid username and password');
res.status(404).type('application/problem+json').send();
} else {
res.type('application/json').status(200).send(usr);
}
}
});
});
我正在使用这个 login.ejs 文件
<div class="container panel panel-primary">
<h2 class="h2 text-center">Login</h2>
<form class="form-horizontal" name="loginForm" method="post" action="/user/login">
<div class="form-group">
<label for="username" class="col-sm-offset-2 col-sm-2 control-label">UserName</label>
<div class="col-sm-4">
<input type="text" name="username" class="form-control" id="username" placeholder="username">
</div>
</div>
<div class="form-group">
<label for="password" class="col-sm-offset-2 col-sm-2 control-label">Password</label>
<div class="col-sm-4">
<input type="password" name="password" class="form-control" id="password" placeholder="Password">
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-4 col-sm-4">
<button type="submit" class="btn btn-primary btn-">Sign in</button>
</div>
</div>
</form>
</div>
现在我想使用此代码并想登录系统。
如果用户登录成功,我想重定向到仪表板页面,否则相同的登录页面。
在执行此代码时,api 会返回 JSON 响应,我如何从该响应中决定是重定向到仪表板页面还是登录页面。
如果需要更多意见,请提供帮助,请发表评论。
【问题讨论】:
-
登录失败后为什么要重新跳转到登录页面?登录失败后,您应该接受错误并将其提供给用户,以便他可以修复它 - 无需重新加载整个页面。这是当前最先进的 UX。成功登录后(因此,如果 http-status 为 200 或者您的返回消息包含成功或其他内容),您可以重定向用户。