passportjs TypeError:User.authenticate 不是函数
Posted
技术标签:
【中文标题】passportjs TypeError:User.authenticate 不是函数【英文标题】:passportjs TypeError: User.authenticate is not a function 【发布时间】:2018-07-21 08:05:27 【问题描述】:我正在尝试使用 passport-local 和 passport-local-mongoose 构建一个小的 nodejs、express、mongodb 应用程序,但是由于某种原因,在主 app.js 文件中输入护照的中间件时,我得到了这个错误“TypeError:User.authenticate 不是函数”
我在一个测试项目中尝试了自己的身份验证,它工作得很好,所以也许这与我的代码顺序有关,但我不知道是什么导致了这个问题。我尝试了几次重构,但都无济于事。
下面是 app.js 和 User Schema 的代码,如果需要,Here's 是项目文件的链接。
app.js
const express = require('express'),
app = express(),
path = require('path'),
exphbs = require('express-handlebars'),
methodOverride = require('method-override'),
flash = require('connect-flash'),
session = require('express-session'),
bodyParser = require('body-parser'),
mongoose = require('mongoose'),
LocalStrategy = require("passport-local"),
passport = require("passport"),
mongodb = require("mongodb"),
User = require("./models/User");
// Load routes
const ideas = require('./routes/ideas');
const users = require('./routes/users');
// Connect to mongoose
mongoose.connect('mongodb://localhost/diaries');
mongoose.Promise = Promise;
// Handlebars Middleware
app.engine('handlebars', exphbs(
defaultLayout: 'main'
));
app.set('view engine', 'handlebars');
// Body parser middleware
app.use(bodyParser.urlencoded( extended: false ));
app.use(bodyParser.json());
// Static folder
app.use(express.static(path.join(__dirname, 'public')));
// Method override middleware
app.use(methodOverride('_method'));
// Express session midleware
//PASSPORT CONFIGURATION
app.use(require("express-session")(
secret: "secret",
resave: false,
saveUninitialized: false
));
app.use(passport.initialize());
app.use(passport.session());
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
app.use(flash());
// Global variables
app.use(function(req, res, next)
res.locals.success_msg = req.flash('success_msg');
res.locals.error_msg = req.flash('error_msg');
res.locals.error = req.flash('error');
next();
);
// Index Route
app.get('/', (req, res) =>
const title = 'Welcome';
res.render('index',
title: title
);
);
// About Route
app.get('/about', (req, res) =>
res.render('about');
);
// Use routes
app.use('/ideas', ideas);
app.use('/users', users);
const port = process.env.PORT;
app.listen(port, () =>
console.log(`Server started on port $port`);
);
用户架构
const mongoose = require('mongoose');
const Schema = mongoose.Schema;
const passportLocalMongoose = require("passport-local-mongoose");
// Create Schema
const UserSchema = new Schema(
name:
type: String,
required: true
,
email:
type: String,
required: true
,
password:
type: String,
required: true
,
date:
type: Date,
default: Date.now
);
UserSchema.plugin(passportLocalMongoose, usernameField: "email");
mongoose.model('users', UserSchema);
【问题讨论】:
【参考方案1】:尝试从require('passport-local').Strategy
导入LocalStrategy
喜欢
const LocalStrategy = require('passport-local').Strategy
【讨论】:
以上是关于passportjs TypeError:User.authenticate 不是函数的主要内容,如果未能解决你的问题,请参考以下文章
如何更新passportjs设置的req.user会话对象?