Facebook Callback appends '#_=_' to Return URL

前端 未结 23 1740
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-11-22 14:56

Facebook callback has started appending #_=_ hash underscore to the Return URL

Does anyone know why? What is the solution?

相关标签:
23条回答
  • 2020-11-22 15:15

    I know this reply is late, but if you are using passportjs, you might want to see this.

    return (req, res, next) => {
        console.log(req.originalUrl);
        next();
    };
    

    I have written this middleware and applied it to express server instance, and the original URL I've got is without the "#_=_". Looks like it when we apply passporJS' instance as middleware to the server instance, it doesn't take those characters, but are only visible on the address bar of our browsers.

    0 讨论(0)
  • 2020-11-22 15:16

    Not sure why they're doing this but, you could get around this by reseting the hash at the top of your page:

    if (window.location.hash == "#_=_")
      window.location.hash = "";
    
    0 讨论(0)
  • 2020-11-22 15:17

    I do not see how this problem is related to facebook AJAX. In fact the issue also occurs with JavaScript disabled and purely redirect based logins.

    An example exchange with facebook:

    1. GET <https://www.facebook.com/dialog/oauth?client_id=MY_APP_ID&scope=email&redirect_uri=MY_REDIRECT_URL> RESPONSE 302 Found Location: <https://www.facebook.com/connect/uiserver.php?[...]>  
    2. GET <https://www.facebook.com/connect/uiserver.php?[...]> RESPONSE 302 Found MY_REDIRECT_URL?code=FB_CODE#_  
    3. GET MY_REDIRECT_URL?code=FB_CODE#_  
    

    Happens only with Firefox for me too.

    0 讨论(0)
  • 2020-11-22 15:18

    This would remove the appended characters to your url

    <script type="text/javascript">
     var idx=window.location.toString().indexOf("#_=_"); 
       if (idx > 0) { 
         window.location = window.location.toString().substring(0, idx); 
       } 
    </script>
    
    0 讨论(0)
  • 2020-11-22 15:21

    A workaround that worked for me (using Backbone.js), was to add "#/" to the end of the redirect URL passed to Facebook. Facebook will keep the provided fragment, and not append its own "_=_".

    Upon return, Backbone will remove the "#/" part. For AngularJS, appending "#!" to the return URL should work.

    Note that the fragment identifier of the original URL is preserved on redirection (via HTTP status codes 300, 301, 302 and 303) by most browsers, unless the redirect URL also has a fragment identifier. This seems to be recommended behaviour.

    If you use a handler script that redirects the user elsewhere, you can append "#" to the redirect URL here to replace the fragment identifier with an empty string.

    0 讨论(0)
  • 2020-11-22 15:26

    You can also specify your own hash on the redirect_uri parameter for the Facebook callback, which might be helpful in certain circumstances e.g. /api/account/callback#home. When you are redirected back, it'll at least be a hash that corresponds to a known route if you are using backbone.js or similar (not sure about jquery mobile).

    0 讨论(0)
提交回复
热议问题