Passport.js代码给出错误:必须由子类覆盖authenticate

时间:2018-01-31 07:06:06

标签: javascript express passport.js

我已在 config / passport.js 文件中定义了本地策略 这是战略代码

passport.use('local', new localStrategy({
    userNameField: 'email',
    passwordFiled: 'password',
    passReqToCallback: true
}, function(req, email, password, done){
    console.log("\n\n\n\nInside passport.js")
    User.findOne({'email':email}, function(err, user){
        if(err){
            return done(null, false);
        }
        if(user){
            return done(null, false, {message:"Email is already in use"});
        }

        var newUser = new User();
        newUser.email = email;
        newUser.password = newUser.encryptPassword(password);
        newUser.save(function(err, result){
            if(err){
                return done(err);
            }
            return done(null, newUser);
        });

    });
}));

策略由 routes / users.js 中的注册路由使用 这是路线

router.get('/register', function(req, res, next){
    res.render('user/register',{});
});

router.post('/register', passport.authenticate('local', {
    successRedirect: '/',
    failureRedirect: '/register',
    failureFlash: true
}));

注册视图

<div class="row">   
    <div class="col-md-4 col-md-offset-4">   
        <h1>Sign Up</h1>
        <br><br><br>
        <form action="/users/register" method="post">
            <div class="form-group">
                <label for="EmailAddress">Email Address</label>
                <input class="form-control" type="text" aria-describedby="emailHelp" name="email" placeholder="example@gmail.com">
                <small id="emailHelp" class="form-text text-muted">We will never share your email with anyone else</small>
            </div>
            <div class="form-group">
                <label for="Password">Password</label>
                <input class="form-control" type="password" aria-describedby="passwordHelp" name="password" placeholder="{{{hello}}}">
                <small id="passwordHelp" class="form-text text-muted"></small>
            </div>
            <button type="submit" class="btn btn-primary"> Sign Up </button>
        </form>
    </div>
</div>

访问路线时,即 localhost:3000 / users / register

显示以下错误

Error: Strategy#authenticate must be overridden by subclass
    at Strategy.authenticate (C:\Users\Muhammad Hassam\Documents\Visual Studio 2017\Projects\Shopping Cart\Shopping Cart\node_modules\passport-strategy\lib\strategy.js:21:9)
    at attempt (C:\Users\Muhammad Hassam\Documents\Visual Studio 2017\Projects\Shopping Cart\Shopping Cart\node_modules\passport\lib\middleware\authenticate.js:361:16)
    at authenticate (C:\Users\Muhammad Hassam\Documents\Visual Studio 2017\Projects\Shopping Cart\Shopping Cart\node_modules\passport\lib\middleware\authenticate.js:362:7)
    at Layer.handle [as handle_request] (C:\Users\Muhammad Hassam\Documents\Visual Studio 2017\Projects\Shopping Cart\Shopping Cart\node_modules\express\lib\router\layer.js:95:5)
    at next (C:\Users\Muhammad Hassam\Documents\Visual Studio 2017\Projects\Shopping Cart\Shopping Cart\node_modules\express\lib\router\route.js:137:13)
    at Route.dispatch (C:\Users\Muhammad Hassam\Documents\Visual Studio 2017\Projects\Shopping Cart\Shopping Cart\node_modules\express\lib\router\route.js:112:3)
    at Layer.handle [as handle_request] (C:\Users\Muhammad Hassam\Documents\Visual Studio 2017\Projects\Shopping Cart\Shopping Cart\node_modules\express\lib\router\layer.js:95:5)
    at C:\Users\Muhammad Hassam\Documents\Visual Studio 2017\Projects\Shopping Cart\Shopping Cart\node_modules\express\lib\router\index.js:281:22
    at Function.process_params (C:\Users\Muhammad Hassam\Documents\Visual Studio 2017\Projects\Shopping Cart\Shopping Cart\node_modules\express\lib\router\index.js:335:12)
    at next (C:\Users\Muhammad Hassam\Documents\Visual Studio 2017\Projects\Shopping Cart\Shopping Cart\node_modules\express\lib\router\index.js:275:10)
    at Function.handle (C:\Users\Muhammad Hassam\Documents\Visual Studio 2017\Projects\Shopping Cart\Shopping Cart\node_modules\express\lib\router\index.js:174:3)
    at router (C:\Users\Muhammad Hassam\Documents\Visual Studio 2017\Projects\Shopping Cart\Shopping Cart\node_modules\express\lib\router\index.js:47:12)
    at Layer.handle [as handle_request] (C:\Users\Muhammad Hassam\Documents\Visual Studio 2017\Projects\Shopping Cart\Shopping Cart\node_modules\express\lib\router\layer.js:95:5)
    at trim_prefix (C:\Users\Muhammad Hassam\Documents\Visual Studio 2017\Projects\Shopping Cart\Shopping Cart\node_modules\express\lib\router\index.js:317:13)
    at C:\Users\Muhammad Hassam\Documents\Visual Studio 2017\Projects\Shopping Cart\Shopping Cart\node_modules\express\lib\router\index.js:284:7
    at Function.process_params (C:\Users\Muhammad Hassam\Documents\Visual Studio 2017\Projects\Shopping Cart\Shopping Cart\node_modules\express\lib\router\index.js:335:12)

我已找到解决方案,无法找到与此错误相关的任何详细信息。

2 个答案:

答案 0 :(得分:0)

根据documentation,您应该将passport.authenticate放入包含reqres args的函数中。

试试这段代码:

router.post('/register', ( req, res, next ) => {
    passport.authenticate('local', {
        successRedirect: '/',
        failureRedirect: '/register',
        failureFlash: true
    } )( req, res, next );
} );

答案 1 :(得分:0)

我尝试执行以下操作时遇到相同的错误

import {Strategy as LocalStrategy} from 'passport'

, 但是当我使用

var LocalStrategy   = require('passport-local').Strategy

const LocalStrategy   = require('passport-local').Strategy

一切正常。

只是

1):将带有import的代码行更改为与constvar的行?

2):创建instance of LocalStrategy(我不尝试,但也许也可以)?