会话信息不会四处展开。 Node.js,Express,Passport&阿贾克斯

时间:2013-10-22 07:19:09

标签: ajax node.js session oauth-2.0 passport.js

我正试图让这个工作,但我迷路了。我想将成功或失败的人重定向到提供的网址。

我在请求身份验证时将URL设置到服务器上的会话中。

        // Setup redirect URLs
        if (typeof(req.session) !== 'undefined') {
            if (typeof(req.session.passURL) == 'undefined') {
                req.session.passURL = '';
            }
            if (typeof(req.session.failURL) == 'undefined') {
                req.session.failURL = '';
            }
            if (typeof(req.query.passURL) != 'undefined') {
                req.session.passURL = req.query.passURL;
            }
            if (typeof(req.query.failURL) != 'undefined') {
                req.session.failURL = req.query.failURL;
            }
        } else {
            console.log('No Session?!?');
        }

然后我从oAuth提供者那里请求auth:

                console.log(req.session);
                req.app.passport.use(googleStrategy);
                req.app.passport.authenticate('google', {
                    scope: [
                        'https://www.googleapis.com/auth/userinfo.profile',
                        'https://www.googleapis.com/auth/userinfo.email'
                    ]
                })(req, res);

上面会话的日志给了我:

{ cookie:
   { path: '/',
     _expires: null,
     originalMaxAge: null,
     httpOnly: true,
     proxy: true,
     secure: true,
     key: 'cookie.sid' },
  passport: {},
  passURL: 'google.com',
  failURL: '' }

当Google完成并将用户发送回回调网址时,我会这样做:

                console.log(req.session);
                req.app.passport.authenticate('google', {
                    failureRedirect: req.session.failURL
                })(req, res, function(err) {
                    if (err) {
                        req.app.lib.util.returnJSON(req, res, 200, err);
                    } else {
                        getUser(req, req.session.passport.user, function () {
                            req.session.passport = null;
                            if (typeof(req.session.passURL) != 'undefined' && req.session.passURL != '') {
                                res.redirect(req.session.passURL);
                            } else {
                                req.app.lib.util.returnJSON(req, res, 200, JSON.stringify(req.session.user));
                            }
                        });
                    }
                });

在oAuth回调的日志会话中,我得到:

{ cookie:
   { path: '/',
     _expires: null,
     originalMaxAge: null,
     httpOnly: true,
     proxy: true,
     secure: true,
     key: 'cookie.sid' },
  passport: {} }

根据Chrome,这是客户看到的流程:

enter image description here enter image description here

节点似乎正确设置了会话:

                    app.use(express.session({
                        store: new MongoStore({db: connection}),
                        cookie: {
                            proxy: true,
                            maxAge: null,
                            secure: true,
                            key: 'cookie.sid'
                        },
                        secret: app.get('cookie_secret')
                     }));
            app.use(app.passport.initialize());
            app.use(app.passport.session());
            app.use(app.router);

我不明白为什么会话会丢失。

0 个答案:

没有答案