title: Error in Signup functionality (TypeError: newUser.encryptPassword is not a function)
body: <p>i am trying to implement signup functionality but it is showing a type error in passport but cant find what is the exact error and how to fix it.. sorry if i am too silly because i am really new in js. i am attaching my codes here...</p>
  
  <p>//error is inside config folder in passport.js</p>
  
  <pre><code>var passport = require('passport');
  var User = require('../models/user');
  var LocalStrategy = require('passport-local').Strategy;
  
  passport.serializeUser(function(user, done) {
    done(null, user.id);
  });
  
  passport.deserializeUser(function(id, done) {
    User.findByID(id, function(err, user) {
      done(err, user);
    });
  });
  
  passport.use('local.signup', new LocalStrategy({
    usernameField: 'email',
    passwordField: 'password',
    passReqToCallback: true
  }, function(req, email, password, done) {
    User.findOne({
      'email': email
    }, function(err, user) {
      if (err) {
        return done(err);
      }
      if (user) {
        return done(null, false, {
          message: 'Email is already in use.'
        });
      }
      var newUser = new User();
      newUser.email = email;
      newUser.password = newUser.encryptPassword(password);
      newUser.save(function(err, result) {
        if (err) {
          return done(err);
        }
        return done(null, newUser);
      });
  
    });
  }));
  </code></pre>
  
  <p>// this is user schema, user.js inside module</p>
  
  <pre><code>var mongoose = require('mongoose');
  var Schema = mongoose.Schema;
  var bcrypt = require('bcrypt-nodejs');
  
  
  var userSchema = new Schema({
    email: {
      type: String,
      required: true
    },
    password: {
      type: String,
      required: true
    },
  
  });
  
  userSchema.method.encryptPassword = function(password) {
    return bcrypt.hashSync(password, bcrypt.genSaltSync(5), null);
  };
  
  userSchema.method.validPassword = function(password) {
    return bcrypt.compareSync(password, this.password);
  };
  module.exports = mongoose.model('User', userSchema);
  </code></pre>
  
  <p>//this is the router inside routes folder, index.js</p>
  
  <pre><code>var express = require('express');
  var router = express.Router();
  var csrf = require('csurf');
  var passport = require('passport');
  
  var Boi = require('../models/boi');
  
  var csrfProtection = csrf();
  router.use(csrfProtection);
  
  /* GET home page. */
  router.get('/', function(req, res, next) {
    var boi = Boi.find(function(err, docs) {
      var boiChunks = [];
      var chunkSize = 3;
      for (var i = 0; i &lt; docs.length; i += chunkSize) {
        boiChunks.push(docs.slice(i, i + chunkSize));
      }
      res.render('library/index', {
        title: 'Boipoka',
        boi: docs
      });
    });
  });
  
  router.get('/user/signup', function(req, res, next) {
    var messages = req.flash('error');
    res.render('user/signup', {
      csrfToken: req.csrfToken(),
      messages: messages,
      hasErrors: messages.length &gt; 0
    });
  });
  router.post('/user/signup', passport.authenticate('local.signup', {
    successRedirect: '/user/profile',
    failureRedirect: '/user/signup',
    failureFlash: true
  }));
  
  router.get('/user/profile', function(req, res, next) {
    res.render('user/profile');
  });
  
  module.exports = router;
  </code></pre>
  
  <p>//here is the main app, app.js</p>
  
  <pre><code>var express = require('express');
  var path = require('path');
  var favicon = require('serve-favicon');
  var logger = require('morgan');
  var cookieParser = require('cookie-parser');
  var bodyParser = require('body-parser');
  var expressHbs = require('express-handlebars');
  var mongoose = require('mongoose');
  var session = require('express-session');
  var passport = require('passport');
  var flash = require('connect-flash');
  
  
  var routes = require('./routes/index');
  
  
  var app = express();
  mongoose.connect('mongodb://localhost:27017/boipoka', {
    useNewUrlParser: true
  });
  require('./config/passport');
  
  // view engine setup
  app.engine('.hbs', expressHbs({
    defaultLayout: 'layout',
    extname: '.hbs'
  }));
  app.set('view engine', '.hbs');
  
  app.use(logger('dev'));
  app.use(express.json());
  app.use(express.urlencoded({
    extended: false
  }));
  app.use(cookieParser());
  app.use(session({
    secret: 'mysupersecret',
    resave: false,
    saveUninitailized: false
  }));
  app.use(flash());
  app.use(passport.initialize());
  app.use(passport.session());
  app.use(express.static(path.join(__dirname, 'public')));
  
  app.use('/', routes);
  
  
  
  
  // catch 404 and forward to error handler
  app.use(function(req, res, next) {
    var err = new Error('Not Found');
    err.status = 404;
    next(err);
  });
  
  // error handler
  app.use(function(err, req, res, next) {
    // set locals, only providing error in development
    res.locals.message = err.message;
    res.locals.error = req.app.get('env') === 'development' ? err : {};
  
    // render the error page
    res.status(err.status || 500);
    res.render('error');
  });
  
  module.exports = app;
  </code></pre>
  
  <p>The error message is </p>
  
  <p>events.js:174
        throw er; // Unhandled 'error' event
        ^</p>
  
  <p>TypeError: newUser.encryptPassword is not a function</p>