gpt4 book ai didi

javascript - 如何修复 TypeError : Cannot read property 'authenticate' of undefined passportjs

转载 作者:搜寻专家 更新时间:2023-10-31 23:32:56 28 4
gpt4 key购买 nike

我正在处理 Node Passport 登录身份验证,但我遇到了这个错误,这让我很难运行我的 Node 服务器

/home/emil/Documents/myapp/app/routes.js:14 app.post('/login', passport.authenticate('local-login', { ^类型错误:无法读取未定义的属性“验证”

我该如何解决?

我的代码:服务器.js

var express = require('express');
var app = express();
var port = 8080;

var cookieParser = require('cookie-parser');
var session = require('express-session');
var morgan = require('morgan');
var mongoose = require('mongoose');
var bodyParser = require('body-parser');
var passport = require('passport');

var configDB = require('./config/database.js');
mongoose.connect(configDB.url);

app.use(passport.initialize());
app.use(passport.session());
app.use(morgan('dev'));
app.use(cookieParser());
app.use(bodyParser.urlencoded({
extended: false
}));
app.use(session({
secret: 'anystringoftext',
saveUninitialized: true,
resave: true
}));

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

//app.use('/', function(req, res){
// res.send('Our First Express program!');
// console.log(req.cookies);
// console.log('===============');
// console.log(req.session);
//});

require('./app/routes.js')(app);

app.listen(port);
console.log('Server running on port: ' + port);

路由.js:

var User = require('./models/user');

module.exports = function (app, passport) {
app.get('/', function (req, res) {
res.render('index.ejs');
});

app.get('/login', function (req, res) {
res.render('login.ejs', {
message: req.flash('loginMessage')
});
});

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

app.get('/signup', function (req, res) {
res.render('signup.ejs', {
message: req.flash('signupMessage')
});
});

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

app.get('/profile', isLoggedIn, function(req, res){
res.render('profile.ejs', { user: req.user });
});

app.get('/:username/:password', function (req, res) {
var newUser = new User();
newUser.local.username = req.params.username;
newUser.local.password = req.params.password;
console.log(newUser.local.username + " " + newUser.local.password);
newUser.save(function (err) {
if (err)
console.log(err);
});
res.send("Success!");
})
};

function isLoggedIn(req, res, next) {
if(req.isAuthenticated()){
return next();
}
res.redirect('/login');
}

Passport .js

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

var User = require('../app/models/user');

module.exports - function(passport) {

passport.serializeUser(function(usser, 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: 'email',
passwordField: 'password',
passReqToCallback: true
},
function(req, email, password, done){
process.nextTick(function(){
User.findOne({'local.username': email}, function(err, user){
if(err)
return done(err);
if(user){
return done(null, false, req.flash('signupMessage', 'That account exists'));
} else {
var newUser = new User();
newUser.local.username = email;
newUser.local.password = password;

newUser.save(function(err){
if(err)
throw err;
return done(null, newUser);
})
}
})

});
}));

passport.use('local-login', new LocalStrategy({
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true
},
function(req, email, password, done){
process.nextTick(function(){
User.findOne({ 'local.username': email}, function(err, user){
if(err)
return done(err);
if(!user)
return done(null, false, req.flash('loginMessage', 'No user found'));
if(user.local.password != password)
return done(null, false, req.flash('loginMessage', 'invalid password'));
}
return done(null, user);
})
})
}
))

最佳答案

您还没有将 passport 传递给路由。 require('./app/routes.js')(app); 应该是 require('./app/routes.js')(app, passport)服务器.js;

关于javascript - 如何修复 TypeError : Cannot read property 'authenticate' of undefined passportjs,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29838952/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com