gpt4 book ai didi

javascript - Express/passport - Passport.authenticate 不是一个函数

转载 作者:太空宇宙 更新时间:2023-11-04 00:30:55 26 4
gpt4 key购买 nike

我是 Passport.js 的新手。我一直在关注以下博客来注册用户

https://scotch.io/tutorials/easy-node-authentication-setup-and-local

但是每次我运行服务器并加载 /register 页面时,它都会显示上述错误

TypeError: passport.authenticate is not a function at module.exports (/home/punit/Documents/****/routes/index.js:10:34) at Layer.handle [as handle_request] (/home/punit/Documents/****/node_modules/express/lib/router/layer.js:95:5) at trim_prefix (/home/punit/Documents/****/node_modules/express/lib/router/index.js:312:13) at /home/punit/Documents/****/node_modules/express/lib/router/index.js:280:7 at Function.process_params (/home/punit/Documents/****/node_modules/express/lib/router/index.js:330:12) at next (/home/punit/Documents/****/node_modules/express/lib/router/index.js:271:10) at /home/punit/Documents/****/node_modules/connect-flash/lib/flash.js:21:5 at Layer.handle [as handle_request] (/home/punit/Documents/****/node_modules/express/lib/router/layer.js:95:5)

我已经检查了/routes/index.js 文件中填充的变量。

var mongoose = require('mongoose');
var passport = require('passport');
var flash = require('connect-flash');
var session = require('express-session');

var routes = require('./routes/index');
var users = require('./routes/users');

mongoose.connect("localhost:27017/****");

require('./models/strategies')(passport)

var app = express();

session 初始化如下

app.use(session({
secret: '*******',
resave: true,
saveUninitialized: false
})); // session secret
app.use(passport.initialize());
app.use(passport.session()); // persistent login sessions
app.use(flash()); // use connect-flash for flash messages stored in session


app.use('/', routes);
app.use('/users', users);
app.use('/register', routes);

require('./routes/index')(app, passport)

strategies.js文件如下

var LocalStrategy = require('passport-local').Strategy
var Profile = require('./profile.js')

var ex = function (passport) {

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

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

passport.use('register', new LocalStrategy({
usernameField: 'email',
passwordField: 'password',
passReqToCallback: true
},
function(req, mobile, password, done) {
process.nextTick(function() {
return done(null, false);

});
}))
}

console.log(ex)

module.exports = ex;

routes/index.js 文件如下

module.exports = function (app, passport) {
/* GET home page. */
app.get('/login', function(req, res, next) {
res.render('index', { title: 'Express' });
});
app.get('/register', function(req, res, next) {
res.render('register', { title: 'Express' });
});

app.post('/register', passport.authenticate('register', {
successRedirect: '/profile',
faliureRedirect: '/register',
faliureFlash: true // allows flash message
}), function(req, res, err) {
console.log(req)
})

}

任何形式的帮助将不胜感激。

最佳答案

您应该使用路由文件中的路由器或传递应用程序

只有一个可以工作。

//app.use('/', routes);
//app.use('/users', users);
//app.use('/register', routes);

require('./routes/index')(app, passport)

这样, Passport 对象将在路线文件中可用。

关于javascript - Express/passport - Passport.authenticate 不是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40958812/

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