Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/excel/28.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Node.js 在未登录时尝试访问路由时使用connect flash消息重定向_Node.js_Express_Passport.js_Connect Flash - Fatal编程技术网

Node.js 在未登录时尝试访问路由时使用connect flash消息重定向

Node.js 在未登录时尝试访问路由时使用connect flash消息重定向,node.js,express,passport.js,connect-flash,Node.js,Express,Passport.js,Connect Flash,当有人试图访问我的管理页面而未进行身份验证时(即有人试图绕过登录页面),我想重定向到带有错误消息的登录页面 这是我的admin终点: server.get('/admin', isLoggedIn, function (req, res) { console.log('Trying to access admin section') res.render('admin', { user: req.user //get the user out of session

当有人试图访问我的管理页面而未进行身份验证时(即有人试图绕过登录页面),我想重定向到带有错误消息的登录页面

这是我的
admin
终点:

server.get('/admin', isLoggedIn, function (req, res) {
    console.log('Trying to access admin section')
    res.render('admin', {
        user: req.user //get the user out of session and pass to template
    })
});
它包含以下
isLoggedIn
中间件:

function isLoggedIn(req, res, next) {
    if (req.isAuthenticated())
        return next();
   console.log('Someone is trying to access this page without being authenticated')
    req.flash('loginMessage', 'You need to be authenticated to access this page');
    console.log(req.flash('loginMessage'))
    res.redirect('/login')
}
登录
访问点定义如下:

server.get('/login', function (req, res) {
    console.log('Using login route');
    res.render('login',
        {message: req.flash('loginMessage')}
        );
});
我的问题是,当有人试图直接访问
admin
页面时,flash消息不会显示。 但是,当尝试使用假凭据登录时,错误消息确实会显示在登录页面中。 有关信息,以下是如何设置我的登录后路线:

server.post('/login', passport.authenticate('local-login', {
    successRedirect:'/admin', // redirect to the secure profile section
    failureRedirect:'/login', //redirect back to the login page if there is an error
    failureFlash: true //allow Flash messages
}));
我在终端中得到以下信息:

Someone is trying to access this page without being authenticated
[ 'You need to be authenticated to access this page' ]
GET /admin 302 8.859 ms - 68
Using login route
GET /login 200 79.373 ms - 1930

在连接闪存中,当您使用
req.flash()
检索按键上设置的闪存消息时,它会将
的消息复制到临时阵列,从连接闪存的内部闪存消息存储中删除该
的消息,然后返回该临时阵列

因此
flash('loginMessage')
在路由'/login'处返回空,因为您以前在
isLoggedIn
console.log(req.flash('loginMessage'))
处检索过它


我在检查connect flash的来源时发现了它。在这里:。在这里的提示应该很快地告诉你这个想法。

< P>如果有人来了,并且所选的答案不起作用,试着尝试下面的方法。我也遇到过同样的问题,但在任何阶段都没有使用console.log等“消费”密钥

代码有问题的版本如下。我在邮寄路线中称这些指示为:

req.flash('errorMessage', errors.array().map(err => err.msg););
res.redirect('/profile');
其中,“profile”的GET路由呈现一个EJS模板,其输入中包含
errorMessage:req.flash('errorMessage')

对我有效的方法是将我的错误消息(
errors.array().map(err=>err.msg)
)分配给一个变量,并将该变量传递给connect flash,如下所示:

var errMsgs = errors.array().map(err => err.msg);
req.flash('errorMessage', errMsgs);
res.redirect('/profile');

希望这有帮助。

现在,未经身份验证的用户可以访问管理页面吗?不,未经身份验证的用户不能访问管理页面。我的问题不在于身份验证,而在于将connect flash消息传递到我的重定向。您是否能够使用其他简单路由显示flash?例如,在“/setflash”设置一个flash,并将其重定向到“/flash”并在那里查看。我不确定我是否理解您的评论,当我在登录页面中输入错误的密码或使用数据库中尚未注册的用户名登录时,我确实显示了正确的flash消息,同样,如果我在注册页面上,并且我使用了一个已经使用过的登录名,就会显示flash消息。好吧,我只是想知道你是否正确设置了flash,因为代码中没有其他地方使用它(除了passport,我认为它有不同的flash,但我检查时错了)。