使用Passport.JS进行“未解决的功能或方法”

时间:2017-04-26 23:26:26

标签: javascript node.js

在设置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);
  

当我运行服务器时,我也收到错误:

TypeError: passport.serialize is not a function

2 个答案:

答案 0 :(得分:0)

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);

答案 1 :(得分:0)

所以,现在,您只需要创建一个控制器来处理用户注册的业务逻辑等等。例如,

* 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);

希望这会有所帮助。不是试图让你改变你的代码库,但这是你做你想做的更好的方式。