gpt4 book ai didi

javascript - Passport - TypeError : passport. 验证不是一个函数

转载 作者:行者123 更新时间:2023-11-28 18:02:43 29 4
gpt4 key购买 nike

我尝试对我的应用程序实现 passport 身份验证策略,但在尝试登录后不断收到以下错误:

TypeError: passport.authenticate is not a function at exports.signin

我很确定我错过了一些东西,但不知道那会是什么。由于 user 模型中没有任何与身份验证相关的内容,因此我决定不在此处输出它。

我的文件夹结构如下:

|-- project
|-- .env
|-- index.html
|-- package.json
|-- server.js
|-- app
| |-- config
| | |-- passport.js
| | |-- routes.js
| | |-- test.js
| | |-- passport
| | |-- local.js
| |-- controllers
| | |-- users.js
| |-- models
| |-- user.js

这是我的 routes.js 文件

/* Config / Routes */

'use strict';
const users = require('../controllers/users');

module.exports = function(app) {
app.post('/login', users.signin);
app.post('/users', users.create);
};

这是我的 LocalStrategylocal.js:

/* Config / Passport / Local */

'use strict';
const mongoose = require('mongoose');
const LocalStrategy = require('passport-local').Strategy;
const User = mongoose.model('User');

module.exports = new LocalStrategy({
usernameField: 'email'
},
function(email, password, done) {
User.findOne({email: email }, function (err, user) {
if (err)
return done(err);

if (!user)
return done(null, false, { message: 'Unknown user' });

if(!user.validPassword(password))
return done(null, false, { message: 'Invalid password' });

return done(null, user);
});
}
);

这是我的 passport.js 文件,其中 LocalStrategy 已分配给 passport:

/* Config / Passport */

'use strict';
const mongoose = require('mongoose');
const User = mongoose.model('User');
const local = require('./passport/local');

module.exports = function(passport) {
passport.use(local);
};

这是我的 users.js Controller 文件:

/* Controller / Users */

'use strict';
const mongoose = require('mongoose');
const User = mongoose.model('User');
const only = require('only');
const passport = require('../config/passport');

/* Creates a new user */
exports.create = function(req, res) {
// create a new user object of request body
const user = new User(only(req.body, 'firstName lastName email password'));

// attempting to create a new user
user.save(function (err, user) {
if(err) {
// output validation errors
return res.json(err);
}
// output created user
return res.json(user);
});
};

exports.signin = function(req, res, next) {
passport.authenticate('local', function (err, user, info) {
if (err)
return next(err);

if (!user)
return res.status(401).json({message: info.message});

req.logIn(user, function(err) {
next(err)
});

res.json(req.user);
})(req, res, next)
};

最后,我的 server.js 文件:

/* Server.js */

'use strict';
const mongoose = require('mongoose');
const bodyParser = require('body-parser');
const config = require('./app/config/test');
const express = require('express');
const passport = require('passport');

const port = process.env.PORT || 3000;
const app = express();

mongoose.Promise = global.Promise;
if(mongoose.connect(config.db)) {
console.log("Connected to a database");
}

/* Database Models */
const User = require('./app/models/user');

app.use(bodyParser.urlencoded({ extended: true }));
app.use(bodyParser.json());

app.use(passport.initialize());

/* Bootstrap Routes */
require('./app/config/passport')(passport);
require('./app/config/routes')(app);

app.listen(port);
console.log('Express app started on port ' + port);

// log the current API version
console.log('Current API version: ' + process.env.API_VERSION);

最佳答案

在您的 users.js 文件中,更改以下行

const passport = require('../config/passport');

const passport = require('passport');

我也遇到了同样的问题。我们必须导入库,而不是我们在config文件夹中创建的passport.js。

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

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