Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/flash/4.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
Javascript 如何在passport js中添加登录错误flash消息_Javascript_Flash - Fatal编程技术网

Javascript 如何在passport js中添加登录错误flash消息

Javascript 如何在passport js中添加登录错误flash消息,javascript,flash,Javascript,Flash,我希望在我的登录表单上显示一条flash消息,当用户在表单中提供了错误的凭证时,但是我不知道在哪里添加flash,我有点困惑。周围的人请帮忙 app.post('/login',passport.authenticate("local", { successRedirect: "/", failureRedirect: "/login" }), function(req, res){ req.flash('error', err.message) }) 我仍然希望它显示表单中的错误。我是新来的

我希望在我的登录表单上显示一条flash消息,当用户在表单中提供了错误的凭证时,但是我不知道在哪里添加flash,我有点困惑。周围的人请帮忙

app.post('/login',passport.authenticate("local", {
successRedirect: "/",
failureRedirect: "/login"
}), function(req, res){
req.flash('error', err.message)
})
我仍然希望它显示表单中的错误。我是新来的 帮助

req.flash('error', err.message)

您应该像这样将消息设置为
res.locals

req.flash('error', 'This is an error message');
res.locals.message = req.flash();
然后,您可以在html中显示它们,如

<div class="error-message">
    <strong> <%=message.message%> </strong>
</div>



我希望它能帮助您。

在服务器端使用以下代码:

    var passport = require('passport');    
    var flash = require('connect-flash');
    app.use(flash());
    app.use(function(req, res, next) {
        res.locals.success_msg = req.flash('success_msg');
        res.locals.error_msg = req.flash('error_msg');
        res.locals.error = req.flash('error');
        next();
    });
您可以在您的
passport.js
中添加消息以弹出:

req.flash('error_msg', 'That email is already taken.');
req.flash('success_msg', 'This is successfull');
在您的前端代码中(根据您的语言。我使用手柄作为前端模板)显示您的警报消息:

    {{#if success_msg}}
        <div class="alert alert-success">{{success_msg}}</div>
    {{/if}}
    {{#if error_msg}}
        <div class="alert alert-danger">{{error_msg}}</div>
    {{/if}}
    {{#if error}}
        <div class="alert alert-danger">{{error}}</div>
    {{/if}}
{{{#如果成功{u msg}
{{success_msg}}
{{/if}
{{{if error_msg}
{{error_msg}}
{{/if}
{{{#如果出错}}
{{error}}
{{/if}

在登录时,您需要在failureFlash:true中编码,并将其设置为true

app.use(function(req,res,next){
    res.locals.error = req.flash("error");
    res.locals.success = req.flash("success")
    next();
})

app.post("/dashlogin", passport.authenticate("local", {
    successRedirect: "/dashboard",
    failureRedirect: "/dashboard/login",
    failureFlash: true,
}),  function(req,res){

})
然后将其闪存到用户页面中

<% if(error && error.length > 0) { %>
    <div class="alert alert-danger" role="alert">
        <div class="container">
            <%= error %>
        </div>
    </div>
<% } %>
0){%>

我知道如何设置它,但我的困惑是在它重定向回来之前将它添加到登录中,这样flash就会显示@Nikunj Sardaras设置它不是问题,而是将它添加到登录帖子@HOTAM SINGH