Session PassportJS:会话未按预期工作

Session PassportJS:会话未按预期工作,session,passport.js,Session,Passport.js,使用passportJS加载会话时遇到问题。不知何故,每次遇到请求时,都会有一个新会话。SerializeUser函数找不到现有会话,每次都会创建一个新会话。现在我怎么知道这个呢? 1.mongodb中的mysessions表。对于每个请求,将在表中创建两个条目。看起来像这样 { "_id" : "U_MhBL17rMVdFbuXt7Y5RGjZeHR5mP7O", "session" : { "cookie" : { "originalM

使用passportJS加载会话时遇到问题。不知何故,每次遇到请求时,都会有一个新会话。SerializeUser函数找不到现有会话,每次都会创建一个新会话。现在我怎么知道这个呢?
1.mongodb中的
mysessions
表。对于每个请求,将在表中创建两个条目。看起来像这样

{
    "_id" : "U_MhBL17rMVdFbuXt7Y5RGjZeHR5mP7O",
    "session" : {
        "cookie" : {
            "originalMaxAge" : 2419200000,
            "expires" : ISODate("2016-06-14T14:32:30.721Z"),
            "secure" : null,
            "httpOnly" : true,
            "domain" : null,
            "path" : "/"
        },
        "passport" : {

        }
    },
    "expires" : ISODate("2016-06-14T14:32:30.721Z")
}
{
    "_id" : "fSfITl6hGLdvny1PVZ3iJ6_dFzTmNJj3",
    "session" : {
        "cookie" : {
            "originalMaxAge" : 2419200000,
            "expires" : ISODate("2016-06-14T14:32:30.808Z"),
            "secure" : null,
            "httpOnly" : true,
            "domain" : null,
            "path" : "/"
        },
        "passport" : {
            "user" : "573b11e32147fec27aa9534e"
        }
    },
    "expires" : ISODate("2016-06-14T14:32:30.808Z")
}
  • 从未调用反序列化用户。
    但我不明白为什么不叫它。我看到了,但我想我已经把所有的事情都解决了 这是我的environment.js文件

    var MongoDBStore = require('connect-mongodb-session')(session);
    
        var sessionStore = new MongoDBStore({
            uri: "mongodb://localhost:27017/metaiotAdmin", // Development mode
            collection: 'mysessions'
        });
        sessionStore.on('error', function(error) {
            assert.ifError(error);
            assert.ok(false);
        });
        /*session options to be given to express. It's really express keeping the sessions and not passport*/
        var sessionOpts = {
            saveUninitialized: true,
            resave: false,
            store: sessionStore,
            secret: "cat at my keyboard",
            cookie: {
                httpOnly: true,
                maxAge: 2419200000
            }
        };
        /*declaring all my global variables and dependencies*/
    
        app.use(cookieParser("cat at my keyboard")); // Secret should be kept in a config file and the folder should be added in gitignore 
        app.use(bodyParser.json());
        app.use(bodyParser.urlencoded({
            extended: true
        }));
        app.use(bodyParser.text({
            type: "text/plain"
        }));
        app.use(session(sessionOpts));
        app.use(passport.initialize());
        app.use(passport.session());
    
    我的login.js

     passport.serializeUser(function(user, done) {
            console.log("Serialize", user.id);
            done(null, user.id);
        });
    
        passport.deserializeUser(function(_id, done) {
            console.log("deserializeUser");
            Users.findById(_id, function(err, user) {
                console.log(err);
                done(err, user);
            });
        });
    
        passport.use('local-login', new LocalStrategy({
            passReqToCallback: true
        }, function(req, username, password, done) {
            Users.findOne({
                'emailId': username
            }, function(err, user) {
                if (err)
                    return done(err);
                if (!user) {
                    return done(null, false, {
                        message: 'Username does not exist'
                    });
                } else if (password === user.password) {
                    console.log("User is verified");
                    req.session.save();
                    return done(null, user);
    
                } else
                    return done(null, false, {
                        message: "Password does not match"
                    });
            });
        }));
    
        app.post('/auth/login', passport.authenticate('local-login'), function(req, res, next) {
            res.sendStatus(200);
        });
    

    我看不出有什么大问题。非常感谢您的帮助。

    请检查您的后端是否存在问题。尝试从邮递员那里发送请求。如果它仍然这样做,这意味着它是一个后端问题,可能你的配置是混乱的地方。如果不是。这意味着邮递员可以发送正确的请求,但你不能。这意味着问题在你的前端

    所以我仔细检查,发现我愚蠢地从我的角度代码中删除了两行

    $httpProvider.defaults.withCredentials = true;
    $httpProvider.defaults.useXDomain = true;
    
    这些行很重要,因为它们设置了头,头反过来运行反序列化程序。这两行是自动为邮递员添加的,但不是为我添加的。在看到邮递员和浏览器的请求和响应后,我意识到了这一点


    如果您有任何其他疑问,请让我知道,以便检查您的后端是否存在问题。尝试从邮递员那里发送请求。如果它仍然这样做,这意味着它是一个后端问题,可能你的配置是混乱的地方。如果不是。这意味着邮递员可以发送正确的请求,但你不能。这意味着问题在你的前端

    所以我仔细检查,发现我愚蠢地从我的角度代码中删除了两行

    $httpProvider.defaults.withCredentials = true;
    $httpProvider.defaults.useXDomain = true;
    
    这些行很重要,因为它们设置了头,头反过来运行反序列化程序。这两行是自动为邮递员添加的,但不是为我添加的。在看到邮递员和浏览器的请求和响应后,我意识到了这一点

    如果您有任何其他疑问,请告诉我