auth.statusChange doesn't fire during FB.init if the user isn't logged in to facebook

后端 未结 3 751
天命终不由人
天命终不由人 2021-02-05 11:14

I wondered if anyone has found a workaround to the behaviour that I\'m experiencing.

Take the example of the below code:



        
3条回答
  •  刺人心
    刺人心 (楼主)
    2021-02-05 11:25

    A better solution that theoretically should save you a roundtrips is as follows (coffeescript, but easily translatable to javascript):

    FB.init
      appId: appId
      channelUrl: channelUrl
      status: true     # Check Facebook Login status on init
      cookies: true
      xfbml: false
    
    FB.getLoginStatus (response) =>
      @parseResponse(response)
      FB.Event.subscribe 'auth.statusChange', @parseResponse
      FB.Event.subscribe 'auth.authResponseChange', @parseResponse
    

    We're still using a manual getLoginStatus to fire when the user is unknown, but this time we still use 'status: true' so that the login status is already cached when getLoginStatus is called. By subscribing to the relevant events only after getLoginStatus has fired, we make sure the handling method parseResponse is only called once on load.

提交回复
热议问题