FB.getLoginStatus returns status unknown

前端 未结 10 1194
不思量自难忘°
不思量自难忘° 2020-12-05 14:20

When calling FB.getLoginStatus using a valid Facebook App the response status is always unknown. Exact response is {authResponse: undefined, status: \"unknown\"}.

         


        
相关标签:
10条回答
  • In my case I was using Brave browser which didn't allow cookies due to which it was giving status unknown. I switched to chrome and now it is working.

    0 讨论(0)
  • 2020-12-05 15:05

    The final answer

    Ok so I think I've finally figured this damn issue out.

    What you need to know:

    1) You authenticate with Facebook via an app ID. This sets various cookies, where your app ID is tagged on the end:

    • fblo_000000000000
    • fbm_000000000000
    • fbsr_000000000000

    2) If you delete these cookies, you're still authenticated to facebook as a regular user (unless you log out completely). And on Facebook's servers they still know you are authorized on this app.

    • So when you run FB.getLoginStatus() again it will just recreate them and put them back again. This is not what your user expects. This is bad. They clicked 'Log out'.

    3) The docs explicitly say this:

    The user is either not logged into Facebook or explicitly logged out of your application so it doesn't attempt to connect to Facebook and thus, we don't know if they've authenticated your application or not. (unknown)

    So they don't even TRY to check if this cookie is set. That's why you get null or undefined. So the fblo cookie is considered like an 'opt-out'. You're NOT ACTUALLY LOGGED OUT by what any regular person would consider being logged out as to mean. There's just a cookie saying you are! Of course if the user logs back in with the login button then the cookie will be deleted, which is what you and your user wants.

    Therefore I believe the only thing that makes sense to do (if you truly need to know the user's status) is to:

    • Manually check the existance of fblo_<APPID> cookie before you run FB.getLoginStatus.
    • If the cookie doesn't exist then do nothing and run your normal flow.
    • If the cookie does exist you have several options :

    1) Option 1

    Do absolutely nothing. You now understand the issue, you understand not to delete the cookie and perhaps you don't need to do anything other than show the Facebook login button.

    2) Option 2

    You assume the person is a user of your app, and do whatever you need to do to show the UI. But you won't be running your full normal logic - this will be application specific to you .

    3) Option 3

    • Manually set the cookie value fblo_<APPID> to n (instead of 'y'). Remember this cookie is set on your domain so you're allowed to change it. *Depending upon your development environment this will vary, but it needs to be done client side, and you may need to specify path of `/' and your cookie domain).
    • Run the getLoginStatus(..., true) - it will not be blocked now because the cookie is now n. However you must not run your normal logic here - because all you want to do is check if the user is actually a Facebook user and/or still authenticated with your app.
    • Manually set the cookie value back to y

    Unfortunately I can't recommend this solution due to weird race conditions. It almost almost works, but FB could deliberately or accidentally break it at any time - and it actually ends up still getting confused thinking you're really logged in when you aren't. Plus it could have all kinds of complications with other plugins I haven't even observed.

    I wish the getLoginStatus would just always run and give accurate information, but this is the only true solution I see. And it's been this way for 8 years so I think we're stuck with it.

    4) Option 4 - Set your own cookie

    • You always have the option to set your own cookies to indicate certain things, like 'this user has at some point logged into Facebook' or 'this user is authenticated with my app'. But I'm cautious about doing this because it can quickly get complicated.

    Incidentally I revisited this issue because I'm using the Messenger plugin for my website and I don't want it to display at all unless I know the person has a Facebook account. So I need an accurate response to login status.

    0 讨论(0)
  • 2020-12-05 15:08

    This was happening to me until I turn on my browser to allow third-party websites to save and read cookies.

    To do this go to

    settings > advanced > site settings > Cookies and site data
    

    Then uncheck the option blocking third-party websites from saving and reading cookies. This might not be a good option but it solves these issues for me.

    0 讨论(0)
  • 2020-12-05 15:09

    For me this meant "on my login page" I needed to specify cookies.

      window.fbAsyncInit = function() {
       FB.init({
          version: 'v2.8',
          cookie     : true,
        });
      };
    

    But don't ask me why this was the case. It also fixed it needing to click twice on the login button to actually login, and doesn't even require an appId, seemingly, FWIW...

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