mongoose 中本地护照的身份验证错误
Posted
技术标签:
【中文标题】mongoose 中本地护照的身份验证错误【英文标题】:authentication error with passport-local in mongoose 【发布时间】:2020-08-27 05:56:07 【问题描述】:我在 js 和 ejs 中与我的课程助理一起做了一个简单的应用程序,我按照他的所有步骤进行操作,但是当我将中间件放入 get 路由时出现错误
var express = require('express'),
mongoose = require('mongoose'),
passport = require("passport"),
bodyParser = require("body-parser"),
User = require("./models/user.js");
LocalStrategy = require("passport-local"),
passportLocalMongoose = require("passport-local-mongoose"),
app = express()
;
mongoose.connect("mongodb://localhost:27017/auth_demo_app",
useUnifiedTopology: true,
useNewUrlParser: true,
useFindAndModify: false
);
app.set("view engine", 'ejs');
app.use(require("express-session")(
secret: "Rusty is thajaodjo asdoj ofadsj",
resave: false,
saveUninitialized: false
));
app.use(passport.initialize());
app.use(passport.session());
app.use(bodyParser.urlencoded(extended: true));
passport.use(new LocalStrategy(User.authenticate()));
passport.serializeUser(User.serializeUser());
passport.serializeUser(User.deserializeUser());
// ===================
// ROUTES
// ===================
app.get("/", function(req, res)
res.render("home");
);
app.get("/register", function(req, res)
res.render("register");
);
app.post("/register", function(req, res)
var userName = req.body.username;
var password = req.body.password;
User.register(new User(username: userName), password, function(err, user)
if(err)
console.log(err);
return res.render('register');
passport.authenticate("local")(req, res, function()
res.redirect("/secret");
);
);
);
app.get("/logout", function(req, res)
req.logout();
res.redirect("/");
);
app.get("/login", function(req, res)
res.render("login");
);
// app.get("/secret", isLoggedIn, function(req, res)
// res.render("secret");
// );
app.get("/secret", isLoggedIn, function(req, res)
// res.render("secret");
console.log("logged in");
);
app.post("/login", passport.authenticate("local",
successRedirect: "/secret",
failureRedirect: "/login"
), function(req, res)
console.log("success");
);
function isLoggedIn(req, res, next)
if(req.isAuthenticated())
return next();
res.redirect("/login");
function serverStarted()
console.log("Server Started Successfully.");
app.listen(3000, serverStarted);
./models/user.js:
var mongoose = require("mongoose");
var passportLocalMongoose = require("passport-local-mongoose");
var UserSchema = new mongoose.Schema(
username: String,
password: String
);
UserSchema.plugin(passportLocalMongoose);
module.exports = mongoose.model("User", UserSchema);
这是错误:
Error: Failed to deserialize user out of session
at pass (C:\Users\Mohamed Mostafa\Desktop\FTP Synced\Authentication\node_modules\passport\lib\authenticator.js:344:19)
at Authenticator.deserializeUser (C:\Users\Mohamed Mostafa\Desktop\FTP Synced\Authentication\node_modules\passport\lib\authenticator.js:362:5)
at SessionStrategy.authenticate (C:\Users\Mohamed Mostafa\Desktop\FTP Synced\Authentication\node_modules\passport\lib\strategies\session.js:60:10)
at attempt (C:\Users\Mohamed Mostafa\Desktop\FTP Synced\Authentication\node_modules\passport\lib\middleware\authenticate.js:366:16)
at authenticate (C:\Users\Mohamed Mostafa\Desktop\FTP Synced\Authentication\node_modules\passport\lib\middleware\authenticate.js:367:7)
at Layer.handle [as handle_request] (C:\Users\Mohamed Mostafa\Desktop\FTP Synced\Authentication\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\Mohamed Mostafa\Desktop\FTP Synced\Authentication\node_modules\express\lib\router\index.js:317:13)
at C:\Users\Mohamed Mostafa\Desktop\FTP Synced\Authentication\node_modules\express\lib\router\index.js:284:7
at Function.process_params (C:\Users\Mohamed Mostafa\Desktop\FTP Synced\Authentication\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\Mohamed Mostafa\Desktop\FTP Synced\Authentication\node_modules\express\lib\router\index.js:275:10)
at initialize (C:\Users\Mohamed Mostafa\Desktop\FTP Synced\Authentication\node_modules\passport\lib\middleware\initialize.js:53:5)
at Layer.handle [as handle_request] (C:\Users\Mohamed Mostafa\Desktop\FTP Synced\Authentication\node_modules\express\lib\router\layer.js:95:5)
at trim_prefix (C:\Users\Mohamed Mostafa\Desktop\FTP Synced\Authentication\node_modules\express\lib\router\index.js:317:13)
at C:\Users\Mohamed Mostafa\Desktop\FTP Synced\Authentication\node_modules\express\lib\router\index.js:284:7
at Function.process_params (C:\Users\Mohamed Mostafa\Desktop\FTP Synced\Authentication\node_modules\express\lib\router\index.js:335:12)
at next (C:\Users\Mohamed Mostafa\Desktop\FTP Synced\Authentication\node_modules\express\lib\router\index.js:275:10)
注意:此错误仅在我登录时发生,当我未登录时,它会将我重定向到登录页面,所以请任何人为我解决问题
【问题讨论】:
【参考方案1】:我找到了我没有调用反序列化用户的解决方案
passport.serializeUser(User.serializeUser());
passport.serializeUser(User.deserializeUser());
右边:
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());
【讨论】:
以上是关于mongoose 中本地护照的身份验证错误的主要内容,如果未能解决你的问题,请参考以下文章