6

我正在使用 Passport.js 通过 OAuth 向 Google 进行身份验证(我使用的是 passport-google-oauth 策略)。它工作正常,但我目前正在将用户重定向到“/”,我想将它们发送到“/”加上当前的哈希标签。我可以在查询字符串参数中发送哈希值,但我似乎无法将该值设置为我要传递以进行身份​​验证的对象的 callbackURL 属性。

有人可以提供有关正确方法的示例或解释吗?我不喜欢使用查询字符串,这似乎是最直接的路线,但我愿意使用会话变量或其他东西,如果这样更容易或更好的话。

谢谢你。

4

1 回答 1

3

您可以通过将返回 url 存储在会话中来实现此效果。

// server
var app, express;

express = require('express');

app = express();

app.configure(function() {
  app.use(express.cookieSession({secret: 'shh'}));
});

app.get('/auth/twitter', function(req, res, next) {
  // to return to '/#/returnHash', request this url:
  // http://example.com/auth/twitter?return_url=%2F%23%2FreturnHash

  // on the client you can get the hash value like this:
  // encodeURIComponent("/"+window.location.hash)
  req.session.return_url = req.query.return_url;
  next();
}, passport.authenticate('twitter'));

app.get('/auth/twitter/callback', passport.authenticate('twitter', {
  failureRedirect: '/login'
}), function(req, res) {
  var url = req.session.return_url;
  delete req.session.return_url;

  // redirects to /#/returnHash
  res.redirect(url);
});
于 2013-01-04T04:57:03.790 回答