【问题标题】:How can I return to the current hash location using Passport.js?如何使用 Passport.js 返回到当前哈希位置?
【发布时间】:2012-08-30 21:18:53
【问题描述】:

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

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

谢谢。

【问题讨论】:

  • 如果“哈希标签”是指 URL 的片段部分,请注意浏览器在发出请求时从不发送该信息,因此了解它的唯一方法是您自己设置。
  • @ebohlman 这根本不是真的。

标签: node.js authentication hash passport.js callbackurl


【解决方案1】:

你可以通过将返回的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);
});

【讨论】:

    猜你喜欢
    • 2015-05-30
    • 1970-01-01
    • 2018-07-04
    • 2017-02-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多