简体   繁体   English

使用Facebook Oauth时,如何在URL上使用#_ = _停止瞬时重定向?

[英]How can I stop the momentary redirect with the #_=_ on the url when using Facebook Oauth?

I'm using Passport and Angular to log in to a site via Facebook. 我正在使用Passport和Angular通过Facebook登录到站点。 Code looks like: 代码如下:

  facebookStrategy: function() {
    if (!process.env.FACEBOOK_APP_ID) {
      throw new Error("A Facebook App ID is required if you want to enable login via Facebook.");
    }
    if (!process.env.FACEBOOK_APP_SECRET) {
      throw new Error("A Facebook App Secret is required if you want to enable login via Facebook.");
    }
    return new FacebookStrategy({
      clientID: process.env.FACEBOOK_APP_ID,
      clientSecret: process.env.FACEBOOK_APP_SECRET,
      callbackURL: process.env.FACEBOOK_CALLBACK_URL || ("http://localhost:" + process.env.PORT + "/auth/facebook/callback")
    }, function(accessToken, refreshToken, profile, done) {
      var user;
      user = module.exports.findOrCreateOauthUser(profile.provider, profile.id);
      done(null, user);
    });
  }

( http://localhost ? crap, need to fix that, this is some sample code that I'm adapting) http://localhost ?废话,需要解决这个问题,这是我正在适应的一些示例代码)

OK so the solution to this is (jade): 确定,所以解决方案是(玉):

script(type="text/javascript").
  if (window.location.href.indexOf('#_=_') > 0) {
      window.location = window.location.href.replace(/#.*/, '');
  }

That seems fine, but it's an unnecessary redirect. 看起来不错,但这是不必要的重定向。 So the perfectionist in me would like to get rid of it. 因此,我中的完美主义者想摆脱它。 The docs seem to say that redirect_uri is needed here, but I have callbackURL and Passport Facebook lib seems to think they're the same . 文档似乎说这里需要redirect_uri ,但是我有callbackURL和Passport Facebook lib 似乎认为它们是相同的 Still getting the redirection to the meaningless URL even when using callbackURL . 即使使用callbackURL仍然可以重定向到无意义的URL。

Specifically I want to know if it's possible to fix this via Passport instead of page-side javascript that fixes the url. 具体来说,我想知道是否可以通过Passport来解决此问题,而不是通过用于修复网址的网页端JavaScript来解决此问题。 The latter seems hacky. 后者似乎很hack。

This is a bug on Facebook's side, not Passport's. 这是Facebook方面的错误,而不是Passport的错误。 You can't change the hash server-side, so adding this snippet at the top of your JS will do the trick (no need for a redirect): 您无法在服务器端更改哈希,因此在JS顶部添加此代码段即可达到目的(无需重定向):

if (window.location.hash === '_=_') {
  window.location.hash = '';
}

Per the author of Passport-Facebook : 根据Passport-Facebook的作者:

Facebook's OAuth 2.0 implementation has a bug in which the fragment #_=_ is appended to the callback URL. Facebook的OAuth 2.0实现存在一个错误,其中片段#_ = _附加到了回调URL。 This appears to affect Firefox and Chrome, but not Safari. 这似乎会影响Firefox和Chrome,但不会影响Safari。 This fragment can be removed via client-side JavaScript, and @niftylettuce provides a suggested workaround [there is a link]. 可以通过客户端JavaScript删除此片段,而@niftylettuce提供了建议的解决方法[有链接]。 Developers are encouraged to direct their complaints to Facebook in an effort to get them to implement a proper fix for this issue. 鼓励开发人员将其投诉转到Facebook,以使他们为该问题实施适当的修复。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 不直接使用OAuth对话框时,如何从服务器facebook api调整facebook登录名的显示? - How can I adjust the display of a facebook login from my server facebook api when it is not using the OAuth Dialog directly? 文件更改时如何重定向服务器URL? - how i can redirect server url when file change? 如何使用javascript获取重定向网址? - How can I get the redirect url using javascript? 如何使用 AWS lambda 重定向 URL 以进行发布请求? - How can I redirect a URL for post request using AWS lambda? 通过add.php(Facebook)添加页面后,如何将用户重定向到自定义网址 - how can i redirect a user to custom url after adding page through add.php (Facebook) 如何将 url 重定向到另一个 url? - how can I redirect a url to another url? 如何使用JavaScript获取URL的重定向目标? - How can I get the redirect target of a URL using JavaScript? OAuth 2 流程完成后如何重定向父页面? - How can I redirect the parent page after OAuth 2 flow is complete? 如何使用Facebook API for JS获取基于Facebook图像ID的图像源(URL) - How can I get the image source (url) based on facebook image id using Facebook API for JS 如何在JavaScript中自定义Facebook OAuth对话框? - How can I customize facebook oauth dialog in javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM