By kmj217


2019-08-13 19:28:29 8 Comments

I am attempting to create a signup page. When I fill in the fields, I receive the following error: TypeError: User.findById is not a function.

It does still create the account by I get the above error and have to close the connection.

Please let me know if i can provide additional code to help troubleshoot.

My deserialize

//load bcrypt
var bCrypt = require("bcrypt-nodejs");

module.exports = function(passport, user) {
  var User = user;
  var LocalStrategy = require("passport-local").Strategy;

  passport.serializeUser(function(user, done) {
    done(null, user.id);
  }); 

// used to deserialize the user
  passport.deserializeUser(function(id, done) {
    User.findById(id).then(function(user) {
      if (user) {
        done(null, user.get());
      } else {
        done(user.errors, null);
      }
    });
  });

My routes

module.exports = function(app, passport, authController) {
  app.get("/", authController.signin);

  app.get("/account", authController.account);

  app.get("/home", authController.home);

  app.get("/logout", isLoggedIn, authController.logout);

  app.post(
    "/signup",
    passport.authenticate("local-signup", {
      successRedirect: "/home",

      failureRedirect: "/account"
    })
  );
  app.post(
    "/signin",
    passport.authenticate("local-signin", {
      successRedirect: "/home",
      failureRedirect: "/"
    })
  );
  function isLoggedIn(req, res, next) {
    if (req.isAuthenticated()) return next();

    res.redirect("/");
  }
};

My controller

var AuthController = function(models) {
  this.account = function(req, res) {
    res.render("account");
  };
  this.signin = function(req, res) {
    res.render("index");
  };
  this.home = function(req, res) {
    res.render("home");
  };
  this.logout = function(req, res) {
    req.session.destroy(function(err) {
      if (err) throw err;
      res.redirect("/");
    });
  };
};

module.exports = AuthController;

0 comments

Related Questions

Sponsored Content

1 Answered Questions

My code cant redirect by passport js . Authentication issue

2 Answered Questions

[SOLVED] Understanding passport serialize deserialize

1 Answered Questions

[SOLVED] PassportJS in Nodejs never call the callback function

1 Answered Questions

1 Answered Questions

[SOLVED] Serialize and Deserialize User — passport.js

  • 2014-04-11 02:42:48
  • Kinnard Hockenhull
  • 3520 View
  • 4 Score
  • 1 Answer
  • Tags:   passport.js

1 Answered Questions

[SOLVED] Calling and Saving User Account Custom Data

Sponsored Content