首页 > 解决方案 > 反序列化用户时遇到问题

问题描述

我正在尝试创建一个注册页面。当我填写字段时,我收到以下错误: TypeError: User.findById is not a function.

它仍然通过我收到上述错误并必须关闭连接来创建帐户。

如果我可以提供额外的代码来帮助解决问题,请告诉我。

我的反序列化

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

我的路线

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("/");
  }
};

我的控制器

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;

标签: passport.js

解决方案


推荐阅读