![](/img/trans.png)
[英]Passport.js throwing “undefined is not a function” with LocalStrategy
[英]“Unresolved function or method” with Passport.JS
在设置了passport.js和mongoose身份验证之后,我一直在所有相关语句中获得“未解析的函数或方法”。
我尝试使chaches无效并重新启动以及其他解决方法,但问题仍然存在。
我的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);
当我运行服务器时,我也得到错误:
TypeError: passport.serialize is not a function
user.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);
passport.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);
所以,现在,您只需要创建一个控制器来处理用户注册的业务逻辑等等。 例如,
* UserController.js *
// other functions
// register
export function register() {
if (!req.body) {
res.status(500).json({ error: 'All Fields Required.' });
}
const user = new User(req.body)
user.save((err, user) => {
if (err) {
res.status(500).json({ err });
}
res.status(200).json({ user });
})
}
router.js
// other important things to require
const passportService = require('./config/passport');
// Middleware to require login/auth
const requireLogin = passport.authenticate('local', { session: false
});
// your routes go here and you can access requireLogin now, like so:
router.get('/users/:id', requireLogin, getUserById);
router.post('/users', register);
希望这可以帮助。 不试图让你改变你的代码库,但这是一个更好的方式来做你想做的事情。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.