oauthcallback后护照会话消失

制作与Ionic,Angular,nodejs等混合应用程序

用户使用电子邮件和密码login,然后想要将第三方身份validation添加到他们的帐户。
他们被序列化进入会话。
我们使用护照进行检查,如果他们有第三方授权,如果没有发送他们这样做。
当用户回到callbackurl时,我们不知道他们是谁,因为req.session是未定义的。

编辑:我一直在试图简化代码来解决问题的路线。

// require everything and app.use them // this is what I'm using for the session config app.use(session({ secret: 'thisIsASecret', resave: false, saveUninitialized: false, cookie: {secure: true, maxAge: (4*60*60*1000)} })); var user = { // simple user model for testing id: 1, username: 'username', password: 'password', oauthId: null }; passport.serializeUser(function(user, done) { done(null, user.id); }); passport.deserializeUser(function(id, done) { done(err, user); }); // Local Passport passport.use(new LocalStrategy(function(username, password, done) { return done(null, user); })); app.post('/login', passport.authenticate('local'), function(req, res) { console.log(req.session); // Prints out session object with passport.user = 1 res.end(); }); // oauth Passport passport.use(new oauthStrategy({ clientID: ****, clientSecret: ****, callbackURL: 'http://localhost:3000/auth/oauth/callback', passReqToCallback: true }, function(req, accessToken, refreshToken, profile, done) { console.log(req.session); // no passport object in session anymore return done(null, profile); })); app.get('/auth/oauth', passport.authorize('oauth')); app.get('/auth/oauth/callback', passport.authorize('oauth'), function(req, res) { console.log(req.session); // no passport object in session here either res.end(); }); 

在客户端login后,我使用这个,因为常规的http请求方法不起作用。

 window.location.href = 'http://localhost:3000/auth/oauth'; 

编辑2:离子不允许会话显然。 所以我发现你可以使用state参数来发送带有oauth请求的令牌,并返回到callback函数,并使用它将oauth细节链接到用户的帐户。

 app.get('auth/oauth/:token', function(req, res) { passport.authorize('oauth', {state: req.params.token}); }); 

唯一的问题是现在它不会redirect到第三方来授权他们。 刚刚超时…

解决scheme是使用这样的路线,其中使用token来标识用户。

 app.get('auth/oauth/:token', function(req, res, next) { passport.authorize('oauth', {state: req.params.token})(req, res, next); }); 

然后,令牌在callback( req.query.state )中可用,我们可以将新的详细信息添加到我们现有的用户详细信息中。