Manny42
Manny42

Reputation: 648

Unwanted page reload after FB.api call

I have a problem using the Facebook api with coffeescript. Each time I do FB.getLoginStatus and the status is "connected", I call FB.api("/me", (data)->console.log(data)) the page reload automatically without me asking for it.

And another problem is that a get an error from Facebook if place a breakpoint after the console.log call and watch for the value of data.

I would like to know if I do this ok, and overall why do the page reload.

Here is the coffeescript code for the page:

window.fbAsyncInit = ->
  FB.init
    appId: document.getElementById("fb-root").getAttribute("data-app-id")
    channelUrl: document.getElementById("fb-root").getAttribute("data-channel-url")
    status: true,
    cookie: true,
    xfbml: true

  FB.Event.subscribe('auth.login', (response) ->
    window.location = window.location
  )
  FB.Canvas.setAutoGrow()
  FB.getLoginStatus((data) ->
     console.log(data)
     if (data.status == "connected")
      uid = data.authResponse.userID
      accessToken = data.authResponse.accessToken;
      FB.api("/me", (data) ->
        console.log(data) // Here is the last call before the reload
      )
     else if (data.status == "not_authorized")
        console.log("Je suis log a facebook")
        $("#FBConnect").on("click", (e) ->
            FB.login( (response) ->
              if response.authResponse
                console.log('Welcome!  Fetching your information.... ');
                FB.api('/me', (response) ->
                  console.log('Good to see you, ' + response.name + '.');
                )
              else
                console.log('User cancelled login or did not fully authorize.');
            )
        )
      # the user is logged in to Facebook,
      # but has not authenticated your app
     else
        console.log("Je ne suis pas connecte a facebook")
      # the user isn't logged in to Facebook.
  )

PageScript = document.getElementsByTagName("script")[0]
return if document.getElementById("FBScript")
FBScript = document.createElement("script")
FBScript.id = "FBScript"
FBScript.async = true
FBScript.src = "//connect.facebook.net/en_US/all.js"
PageScript.parentNode.insertBefore(FBScript, PageScript)

Upvotes: 0

Views: 312

Answers (1)

Sudhir Bastakoti
Sudhir Bastakoti

Reputation: 100175

Page reload might be due to following line in your code:

FB.Event.subscribe('auth.login', (response) ->
    window.location = window.location
)

auth.login is fired when the auth status changes from unknown to connected, so when you are connected, the code executes and as you are reloading the page within that function, the page gets reloaded. Removing the line:: window.location = window.location should resolve it.

Upvotes: 1

Related Questions