我用js和ejs与我的课程助理一起制作了一个简单的应用程序,我跟着他遵循了所有步骤,但是当我把一个获取路线的中间件放进去时我却出错了
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)
注意:仅当我登录时会出现此错误,而当我未登录时会出现此错误,请重定向到登录页面,请问有人为我解决了什么问题
答案 0 :(得分:0)
我找到了我没有打电话给反序列化用户的解决方案
passport.serializeUser(User.serializeUser());
passport.serializeUser(User.deserializeUser());
右边:
passport.serializeUser(User.serializeUser());
passport.deserializeUser(User.deserializeUser());