我对node.js
和passport.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 +' /');
});
答案 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);
}
}));
我相信这是你的问题。