Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

285
Views
"Unresolved function or method" with Passport.JS

After setting up passport.js and mongoose authentication, I am consistently getting the "Unresolved function or method" on all of my relevant statements.

enter image description here enter image description here

I tried invalidating chaches and restarting along with other workarounds, however, the problem has persisted.

My 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:

TypeError: passport.serialize is not a function

over 4 years ago · Santiago Trujillo
2 answers
Answer question

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);
over 4 years ago · Santiago Trujillo Report

0

So, now, you just need to create a controller to handle the business logic for user registrations and so on. For example,

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

Hope this helps. Not trying to make you change your codebase but this is a better way to do what you want to do.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!