身份验证失败后用 CSS 响应用户。 (护照)
Respond to User with CSS after failed authentication. (Passport)
我有一个网站允许用户通过单击登录来登录,这将弹出一个 Bootstrap 模式而不重定向用户。我在许多网站上看到,如果您登录失败,您将留在该路线上,所有会改变的是将出现一条消息,通知您登录失败。我想用本地护照来做这件事,在身份验证失败后,我可以简单地显示一些 CSS,同时仍然有 Bootstrap 模式。
但是,我似乎只能显示一个 req.flash,似乎我必须在身份验证失败后重定向,否则会显示错误。
router.post("/login", passport.authenticate("local",
{
successRedirect: "/profile",
failureRedirect: "/",
failureFlash : true
})
);
目前,我只能做这些了。是否可以按照我提到的方式进行而不重定向?护照文件没有显示这一点。
当然,你可以随心所欲。来自官方docs:
app.post('/login',
passport.authenticate('local'),
function(req, res) {
// If this function gets called, authentication was successful.
// req.user contains the authenticated user.
res.redirect('/users/' + req.user.username);
});
"By default, if authentication fails, Passport will respond with a 401 Unauthorized status, and any additional route handlers will not be invoked. If authentication succeeds, the next handler will be invoked and the req.user property will be set to the authenticated user."
所以您所要做的就是在客户端逻辑中处理 401 响应,然后根据它显示 message/CSS.
我有一个网站允许用户通过单击登录来登录,这将弹出一个 Bootstrap 模式而不重定向用户。我在许多网站上看到,如果您登录失败,您将留在该路线上,所有会改变的是将出现一条消息,通知您登录失败。我想用本地护照来做这件事,在身份验证失败后,我可以简单地显示一些 CSS,同时仍然有 Bootstrap 模式。
但是,我似乎只能显示一个 req.flash,似乎我必须在身份验证失败后重定向,否则会显示错误。
router.post("/login", passport.authenticate("local",
{
successRedirect: "/profile",
failureRedirect: "/",
failureFlash : true
})
);
目前,我只能做这些了。是否可以按照我提到的方式进行而不重定向?护照文件没有显示这一点。
当然,你可以随心所欲。来自官方docs:
app.post('/login',
passport.authenticate('local'),
function(req, res) {
// If this function gets called, authentication was successful.
// req.user contains the authenticated user.
res.redirect('/users/' + req.user.username);
});
"By default, if authentication fails, Passport will respond with a 401 Unauthorized status, and any additional route handlers will not be invoked. If authentication succeeds, the next handler will be invoked and the req.user property will be set to the authenticated user."
所以您所要做的就是在客户端逻辑中处理 401 响应,然后根据它显示 message/CSS.