Node.js 如何使用Passport.js返回到当前哈希位置?

Node.js 如何使用Passport.js返回到当前哈希位置?,node.js,authentication,hash,passport.js,callbackurl,Node.js,Authentication,Hash,Passport.js,Callbackurl,我使用Passport.js通过OAuth向Google进行身份验证(我使用的是Passport Google OAuth策略)。它工作正常,但我当前正在将用户重定向到“/”,我希望将它们发送到“/”和当前的哈希标记。我可以在查询字符串参数中发送哈希值,但我似乎无法将该值设置为要传递进行身份验证的对象的callbackURL属性 有人能提供一个例子或解释,以正确的方式做到这一点吗?我不必使用查询字符串,它似乎是最直接的方法,但我愿意使用会话变量或其他东西,如果这更容易或更好的话 谢谢。您可以通过

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

有人能提供一个例子或解释,以正确的方式做到这一点吗?我不必使用查询字符串,它似乎是最直接的方法,但我愿意使用会话变量或其他东西,如果这更容易或更好的话


谢谢。

您可以通过在会话中存储返回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);
});

如果“hash tag”指的是URL的片段部分,请注意,浏览器在发出请求时从不发送该信息,因此知道该信息的唯一方法是您自己设置它。@ebohlman这完全是错误的。