本文介绍了节点通行证错误:未知身份验证策略“本地登录";的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用Node.js上的护照进行本地身份验证,据我所知,我的所有代码都是正确的,但我仍然收到有关未知身份验证策略的烦人错误,因此也许有人可以帮助我"遇到这个问题,我的代码如下所示.

I have been trying to get local authentication work with passport on nodejs and as far as i can tell all of my code it is correct but i keep getting the same annoying error about "unknown authentication strategy so maybe someone else can help me with this problem my code is shown below.

这是我在nodejs中配置护照的代码.

Here is my code for passport configuration in nodejs.

var passport = require('passport');
var LocalStrategy = require('passport-local').Strategy;
var User = require('../models/user');

module.exports = function(passport) {


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

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

    passport.use('local-signup', new LocalStrategy({

        usernameField : 'username',
        passwordField : 'password',
        passReqToCallback : true
    },

    function(req, username, password, done) {

        process.nextTick(function() {

            User.findOne({ 'local.username' : username}, function(err, user) {
                if (err)
                    return done(err);

                if(user) {
                    return done(null, false, req.flash('signupMessage', 'That Username is already taken.'));
                }

                else {

                    var newUser = new User();

                    newUser.local.username = username;
                    newUser.local.password = newUser.generateHash(password);


                    newUser.save(function(err) {
                        if(err)
                            throw err;

                        return done(null, newUser);
                    });
                }
            });
        });
    }));



    passport.use('local-login', new LocalStrategy({

        usernameField : 'username',
        passwordField : 'password',
        passReqToCallback : true
    },
    function(req, username, password,done) {

        User.findOne({ 'local.username' : username}, function(err, user) {

            if(err)
                return done(err);

            if(!user)
                return done(null, false, req.flash('loginMessage', 'No user found.'));

            if(!user.validPassword(password))
                return done(null, false, req.flash('loginMessage', 'Oops! Wrong password.'));

            return done(null, user);
        });
    }));

};

这是服务器端的帖子.

app.post('/signin', passport.authenticate('local-login', {
    successRedirect : '/profile',
    failureRedirect : '/login',
    failureFlash : true
}));

这是html文档中的表格

And here is the form in the html doc

<div id="signin">
        <form class="form-horizontal" method="POST" action="/signin">
            <div class="form-group">
            <label class="control-label col-sm-2">Username:</label>
            <div class="col-xs-3">
                <input type="text" class="form-control"></input><br>
            </div>
            </div>
            <div class="form-group">
            <label class="control-label col-sm-2">Password:</label>
            <div class="col-xs-3">
                <input type="password" class="form-control"></input><br><br>
            </div>
            </div>
            <div class="form-group">
            <div class="col-sm-offset-2 col-sm-10">
                <button type="submit" class="btn btn-default btn-lg">Sign In</button><br><br>
            </div>
            </div>
        </form>

        <div id="accountlinks">
            <a href="/signup">Create Account</a><br>
            <a href="">Forgot Password</a>
        </div>
    </div>

任何人都可以通过告诉我我做错了什么来帮助我.谢谢

Can anyone please help me by telling me what i have done wrong. thanks

推荐答案

require('./config/passport')(passport);

更改文件的路径.没有这项工作,护照的配置将不会传递到路线.

Change the path of the file. Without this working, passport's configurations will not be passed to the routes.

以下是该行的位置的摘要:

Here is a snippet of where the line should be located:

// server.js

// configuration
===============================================================
mongoose.connect(configDB.url); // connect to our database

require('./config/passport')(passport); // pass passport for configuration

这篇关于节点通行证错误:未知身份验证策略“本地登录";的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-13 03:01