在我们的view.ejs中传递护照失败消息

时间:2017-02-21 09:21:26

标签: node.js express passport.js ejs

//This is our user.js  (I am using passport here)

passport.use(new LocalStrategy(
    function (username, password, done) {
        User.getUserbyUsername(username, function (err, user) {
            if (err) throw err;
            if (!user) {
                return done(null, false, { message: 'unknown user' });
            }
            User.comparePassword(password, user.password, function (err, isMatch) {
                if (err) throw err;
                if (isMatch) {
                    return done(null, user);
                } else {
                    return done(null, false, { message: 'Invalid password' });
                }
            });
        });

    }));

passport.serializeUser(function (user, done) {
    done(null, user.id);
});

passport.deserializeUser(function (id, done) {
    User.getUserbyId(id, function (err, user) {
        done(err, user);
    });
});

router.post('/login',
    passport.authenticate('local', { failureRedirect: '/users/login', failureFlash: true }),
    function (req, res) {

        ---------------------------------------------------------------------------
            //This is our app.js
            // Passport init
            app.use(passport.initialize());
        app.use(passport.session());

        // Express validator
        app.use(expressValidator({
            errorFormatter: function (param, msg, value) {
                var namespace = param.split('.')
                    , root = namespace.shift()
                    , formParam = root;

                while (namespace.length) {
                    formParam += '[' + namespace.shift() + ']';
                }
                return {
                    param: formParam,
                    msg: msg,
                    value: value
                };
            }
        }));

        //connect Flash
        app.use(flash());

        //this is our login view
        <div class="row">
            <div class="column small-9 medium-6 large-4 small-centered h-text-center">
                <form action="/users/login" method="post" class="login-form">

                    <div id="input-wrap">
                        <div id="error-wrap"><div id="notify-me-error"></div></div>
                        <input class="input text" type="text" id="userName" name="username" placeholder="User Name" />
                    </div>
                    <div id="input-wrap">
                        <div id="error-wrap"><div id="notify-me-error"></div></div>
                        <input class="input text" type="password" id="password" name="password" placeholder="Password" />
                    </div>
                    <div class="input-wrap">

                        <button class="button notify-me" type="submit" class="btn btn-lg btn-primary btn-block">Login</button>

                    </div>
                </form>
            </div>
        </div>

我使用视图引擎作为ejs并进行身份验证。

我正在使用护照。因此,在护照中,他们有自己的变量,用于声明存储在消息中的错误消息。

现在我需要在我的视图中显示这些错误消息。我无法显示这些消息。以前使用把手我能够做到。但是我和ejs面临着问题。任何人都建议我怎么做它?

0 个答案:

没有答案