gpt4 book ai didi

javascript - "Unresolved function or method"与 Passport.JS

转载 作者:太空宇宙 更新时间:2023-11-04 02:07:33 25 4
gpt4 key购买 nike

设置 Passport.js 和 mongoose 身份验证后,我在所有相关语句中始终收到“未解析的函数或方法”。

enter image description here enter image description here

我尝试使缓存无效并重新启动以及其他解决方法,但是问题仍然存在。

我的user.js:

var Schema = mongoose.Schema;
var passportLocalMongoose = require('passport-local-mongoose');

//create user schema and model
var User = new Schema({
username: String,
password: String,
studentID: String,
grades: [{
subject: String,
grade: String
}]
});

User.plugin(passportLocalMongoose);

module.exports = mongoose.model('user', User);

When I run the server, I also get the error:

类型错误:passport.serialize不是一个函数

最佳答案

用户.js

const mongoose = require('mongoose');
const bcrypt = require('bcrypt-nodejs');

const Schema = mongoose.Schema;

//= ===============================
// User Schema
//= ===============================
const UserSchema = new Schema({
email: {
type: String,
lowercase: true,
unique: true,
required: true
},
password: {
type: String,
required: true
}
});


// Pre-save of user to database, hash password if password is modified or new
UserSchema.pre('save', function (next) {
const user = this,
SALT_FACTOR = 5;

if (!user.isModified('password')) return next();

bcrypt.genSalt(SALT_FACTOR, (err, salt) => {
if (err) return next(err);

bcrypt.hash(user.password, salt, null, (err, hash) => {
if (err) return next(err);
user.password = hash;
next();
});
});
});

// Method to compare password for login
UserSchema.methods.comparePassword = function (candidatePassword, cb) {
bcrypt.compare(candidatePassword, this.password, (err, isMatch) => {
if (err) { return cb(err); }

cb(null, isMatch);
});
};

module.exports = mongoose.model('User', UserSchema);

护照.js

const passport = require('passport');
const User = require('../models/user');
const config = require('./main');
const LocalStrategy = require('passport-local');

// username field is now email
const localOptions = {
usernameField: 'email'
};

// set up the local login strategy
const localLogin = new LocalStrategy(localOptions, (email, password, done) => {
User.findOne({ email }, (err, user) => {
if (err) { return done(err); }
if (!user) { return done(null, false, { error: 'Your login details could not be verified. Please try again.' }); }

user.comparePassword(password, (err, isMatch) => {
if (err) { return done(err); }
if (!isMatch) { return done(null, false, { error: 'Your login details could not be verified. Please try again.' }); }

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

passport.use(localLogin);

关于javascript - "Unresolved function or method"与 Passport.JS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43646174/

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