passport.js - 反序列化用户时遇到问题
问题描述
我正在尝试创建一个注册页面。当我填写字段时,我收到以下错误:
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;
解决方案
推荐阅读
- android - Firebase 和 Google-Auth 和 Phone-Auth 的 SHA-1 问题
- php - 在 PHP 中创建会话,然后在 JSP 中检查该会话
- python - Python stringTypeError:必须是 str,而不是 NoneType
- javascript - 如何从 SVG 标签内控制 HTML 元素
- rust - 如何在这个结构定义中避免 PhantomData?
- solr - SolrCloud 的 SolrJ 客户端针对响应速度进行了优化?
- swift - IOS UILabel 在我重新加载应用程序或单击其他位置之前不会更新
- graphql - 来自 withApollo HOC 的 props.client.subscribe 的 API 文档
- php - 在 Laravel 通知电子邮件中添加粗体文本和换行符
- python - 从文件夹结构备份/复制 pip 模块