Express-Session对会话变量所做的任何更改都不会跨路由保存

时间:2020-05-07 05:11:27

标签: node.js express authentication express-session

这是一个关于堆栈溢出的常见问题,我检查了很多帖子,但是没有用。 我试图创建一个登录页面,将User_id(成功登录后)保存在会话变量中,然后使用该变量检查用户是否已登录。在下一部分中,我试图在中间件中实现它。

但是,我的更改并未保存在所有路线中。示例:-

这是我的登录路线(我在会话会话中存储user_id的地方)

router.post("/login", function(req,res){
User.findOne({EnrollNumber: req.body.EnrollNumber}, function(err, user){
    if(err|| !user ||!(bcrypt.compareSync(req.body.password, user.password))){
        console.log("Incorrect Email Password");
    }else{
        console.log("Login is successfull");
        //Setting Up the session
        req.session.userId= user._id;
        console.log(req.session.userId);
    }
    console.log(req.session);
});
res.redirect("/forum");

此处console.log(要求会话)的输出是

Session {
 cookie: { path: '/', _expires: null, 
  originalMaxAge: null, httpOnly: true },
  userId: 5eb3892938...
}

我的中间件

    var middlewareObj ={}
    middlewareObj.isLoggedIn= function(req,res,next){
    console.log("My Session variable is");
    console.log(req.session);
    if(req.session.userId){
        console.log("Session variable is set");
        return next();
    }
    console.log("User is not logged in");
    res.redirect("/login");
}
module.exports = middlewareObj;

输出在这里

    My Session variable is
Session {
  cookie: { path: '/', _expires: null, originalMaxAge: null, httpOnly: true }
}
User is not logged in

结果,即使我已经登录,也无法访问那些页面。

我的app.js

var express = require("express");
var app = express();
var bodyParser = require("body-parser");
var mongoose = require("mongoose");
var methodOverride = require("method-override");
var session = require('express-session');

//Requiring Routes
var forumRoutes = require("./routes/forums");
var threadRoutes = require("./routes/threads");
var indexRoutes = require("./routes/index");
var commentRoutes= require("./routes/comments");  
mongoose.connect("mongodb://localhost/Project_forum");
app.use(bodyParser.urlencoded({ extended: true }));
app.set("view engine", "ejs");
app.use(express.static(__dirname + "/public"));
app.use(methodOverride("_method"));
app.use(session({
   resave: false, // don't save session if unmodified
   saveUninitialized: false, // don't create session until something stored
   secret: 'shhhh, very secret lubba wubba dubba etc etc'
 }));  
app.use("/", indexRoutes);
app.use("/forum", forumRoutes);
app.use("/forum/:id/thread", threadRoutes);
app.use("/forum/:id/thread/:id/comments", commentRoutes);


app.listen(8000, function () {
   console.log("The forum Server Has Started!");
});

1 个答案:

答案 0 :(得分:0)

将登录路径更改为此:-

    router.post("/login", function(req,res){
User.findOne({EnrollNumber: req.body.EnrollNumber}, function(err, user){
    if(err|| !user ||!(bcrypt.compareSync(req.body.password, user.password))){
        console.log("Incorrect Email Password");
        res.redirect("someplace");
    }else{
        console.log("Login is successfull");
        //Setting Up the session
        req.session.userId= user._id;
        console.log(req.session.userId);
        res.redirect("Some Another place (i.e ur secret page)");
    }
});

在会话更改后立即添加重新发送/重定向即可保存它。