2016-05-17 72 views
1

我在使用passportJS加载会话时遇到问题。不知何故,每次遇到新的会话请求。 SerializeUser函数无法找到现有会话并最终每次创建一个新会话。现在我怎么知道这一点?
1.在mongodb中的mysessions表。对于每个请求,表中都创建了两个条目。看起来像这样。PassportJS:会话不按预期工作

{ 
    "_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") 
} 
  1. 反序列化用户不会被调用。
    但我不明白为什么它不被称为。我看过thisthis,但我想我已经完成了所有的整理。

这里是我的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); 
    }); 

我看不出什么可怕的错误。帮助表示赞赏。

回答

0

那么检查你是否真的有问题与你的后端。尝试从邮递员发送请求。如果仍然存在,则意味着它是后端问题,可能是您的配置在某处出现混乱。如果不是。这意味着邮差可以发送适当的请求,但你不能。这意味着问题出现在你的前端。

所以我再次检查,发现我愚蠢地从我的角码删除了两行。

$httpProvider.defaults.withCredentials = true; 
$httpProvider.defaults.useXDomain = true; 

这些行很重要,因为它们设置了头部,然后运行解串器。这两行是为邮差自动添加的,但不适合我。在看到Postman和浏览器的请求和响应后,我意识到了这一点。

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