使用passport.js进行用户身份验证错误

时间:2017-05-30 09:09:12

标签: javascript node.js authentication passport.js

我对node.jspassport.js非常陌生。我正在尝试使用this video了解如何制作身份验证应用,但是在视频中达到31:04后我一直收到此错误。

  

" TypeError:无法读取属性' name'未定义的   Authenticator.use"

这是我的app.js文件:

var express = require('express');
var bodyParser = require('body-parser');
var cookieParser = require('cookie-parser');
var expressSession = require('express-session');

var passport = require('passport');
var passportLocal = require('passport-local');

var app = express();

app.set('view engine', 'ejs');

app.use(bodyParser.urlencoded({ extended: false }));
app.use(cookieParser());
    secret: process.env.SESSION_SECRET
app.use(expressSession({  || 'secret',
    resave: false,
    saveUninitialized: false
}));


app.use(passport.initialize());
app.use(passport.session());

passport.use(passportLocal.Strategy(function(username, password, done){
    //connect to a real db here
    if(username===password){
        done(null,{id: username, name: username});
        //these actually have to bbe pulled from the db
    }
    else{
        done(null,null);
    }
}));


app.get('/', function(req, res){
    res.render('index',{
        isAuthenticated: req.isAuthenticated(),
        user: req.user

    });
});

app.get('/login', function(req, res){
    res.render('login');
});

app.post('/login', passport.authenticate('local' , function(req,res){
    res.redirect('/');
}));



var port = process.env.PORT || 1337;

app.listen(port, function(err,res){
    if(err)
        console.log(err);
    else
        console.log('server started on localhost:'+ port +' /');
});

1 个答案:

答案 0 :(得分:0)

您必须致电new运营商:

passport.use(new passportLocal.Strategy(function(username, password, done){
    //connect to a real db here
    if(username===password){
        done(null,{id: username, name: username});
        //these actually have to bbe pulled from the db
    }
    else{
        done(null,null);
    }
}));

我相信这是你的问题。