3

I have the following snippet of code:

var myParams = { 
'clientid' : 'XXXXX.apps.googleusercontent.com', 
'cookiepolicy' : 'single_host_origin', 
'callback' : _.bind(function(response){ this._loginGoogleCb(response); }, this), 
            'scope' : 'https://www.googleapis.com/auth/plus.me https://www.googleapis.com/auth/plus.profile.emails.read', 
            'requestvisibleactions' : 'http://schemas.google.com/AddActivity' 
        }; 
gapi.auth.signIn(myParams); 

The first time I click the button and this code gets executed, there is no problem.
But when I click the button for a second time, the callback (and only the callback) gets executed twice.
Every time I execute this code, the number of requests to the Google server (and the related callback) increases by 1.

I double checked, the calling function itself gets only executed once when repeated.
The button click itself is not the problem.

Any idea what might be the problem?

Ben Smith
  • 19,589
  • 6
  • 65
  • 93
html_programmer
  • 18,126
  • 18
  • 85
  • 158
  • will it be possible to put a fiddle for your page, to see what exactly is the issue you are facing..or how are you able to confirm that it's the callback to the server that's increasing with every click.. a little more code snippet would also help..and as I know that when you do a Oauth using javascript it is one time authorisation so when you do the Oauth for the very first time it shows the consent screen where you basically accept or deny..and from second time onwards the pop up still appears but you need not do anything..it picks up and get the access token..so the popup comes up everytime – Raghvendra Kumar Dec 05 '14 at 22:11
  • I've created a fiddle here: http://jsfiddle.net/g1mjLkgp/ but this is probably not going to be of much help because of the origin mismatch. The problem is "resolved" since the user will always return to the homepage with a page refresh after logging out. This however is a sad solution to the problem, but it solves it for now. – html_programmer Dec 06 '14 at 00:24

1 Answers1

2

As can be seen from this article, there are three different status methods:

  • null
  • PROMPT
  • AUTO

in the authorization object:

{
  "id_token": string,
  "access_token": string,
  "expires_in": string,
  "error": string
  "status": { /* object */
    "google_logged_in" : boolean,
    "signed_in" : boolean,
    "method" : string /* null, PROMPT, or AUTO */
  }
}

What is happening is that when you are logging-in for the first time just one of these status methods are being triggered ("PROMPT"), but when you press the button again two status methods are triggered ("PROMPT" and "AUTO").

Example "signinCallback" code for dealing with these status methods can be found here.

Also if you are calling underscore's bind funtion multiple times, then the bound function will also be called multiple times. Hence the reason why you are seeing "the number of requests to the Google server (and the related callback) increases with 1." I'd suggest encapsulating this call to bind within another function and include a guard condition to stop this being called multiple times.

Community
  • 1
  • 1
Ben Smith
  • 19,589
  • 6
  • 65
  • 93
  • 1
    Thanks for answer. Well I have the callback code, but what worried me is that it gets triggered an increasing number of times, every time I sign back in after having signed out without a page refresh. I can easily check this by using console.log before checking the status. – html_programmer Dec 08 '14 at 13:55
  • Have you checked the "error" variable in the authorization object when you sign back in (though I doubt there's an error, worth checking anyway). What is the value of "method" in subsequent log-ins? Also the fact that it gets triggered an increasing number of times sounds symptomatic of the callback code getting re-registered multiple times; you sure that you are only registering the callback once? – Ben Smith Dec 08 '14 at 14:11
  • Yes, there are no errors, and the subsequent calls all have status method "PROMPT". It indeed appears that I'm registering the callback multiple times, but I'm not sure how I can register it only once. As my code in the question indicates, it is just an array of parameters that I pass to the `gapi.auth.signIn(myParams);` every time that I want to log in again. – html_programmer Dec 08 '14 at 14:47
  • Ok, in that case why are you using underscore's bind? I suspect that every time this is called you are re-regsitering the handler. Why not remove the call to bind and just reference the handler (as is done in the linked question I mentioned in my answer). – Ben Smith Dec 08 '14 at 15:23
  • I bind the scope to a Marionette Controller Object in which I manage all the logic. – html_programmer Dec 08 '14 at 16:46
  • I'd put a guard condition on your call to bind (i.e. encapsulate your call to bind in another method with a check to see if its already been called) to stop this method from being bound multiple times. It's worth trying out just to check to see if this is what is causing the multiple triggering. – Ben Smith Dec 08 '14 at 16:49
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/66431/discussion-between-bensmith-and-kim-gysen). – Ben Smith Dec 09 '14 at 01:27