I am working on a function which allows users to sign in on my website with their Google account.
My code is based on the Google documentation (others signIn() options are in meta tags).
function login() {
gapi.auth.signIn({'callback':
function (authResult) {
if (authResult['status']['signed_in']) {
console.log('Okay');
}else {
console.log('Error');
}
}
});
}
When I call login(), a Google pop up appears, I approve the terms of my application and everything works fine.
But the callback is called twice :
- 1st case: If I never approved apps permissions then the callback will be call at the opening of the pop up AND when I will approve the permissions. So it will write "Error" and "Okay".
- 2nd case: If I already approved the permissions, it will write "Okay" two times.
I added the option 'approvalprompt': 'force'
to the signIn() function. The callback function is no longer called twice but it forces the user to approve the app's permissions, even if previously approved. So it's not user friendly.
Is there a friendly user way to approve the app's permissions one time without having two callback ?
Thank you.
Try to register first call in some local variable and then process it
This quick solution helps me:
also you can add following code before call gapi.auth.signIn
I am facing the same issue: signin callback called twice in case of user that already granted permission; the local variable approach (initializedGoogleCallback) isn't working for me because it call the callback one time only when the user already granted access, but didn't call it if the user is the new one. After a bit of research (i especially dig in site using the g+ auth) i noticed that all of them use the
'approvalprompt': 'force'
and they have the already granted user to reapprove a "Offline Access" policy everytime. Even the google example i followed to setup my app (https://developers.google.com/+/web/signin/javascript-flow) even if it did not mention it, it uses the "force" parameter. For the moment it seems the only solution if you want to use the javascript flow (that mean if you need a personal style signin button)Like the Drew Taylor's answer, to avoid the double callback with the pure javascript sign in solution, you can check the user's session state:
I think that the callback with the AUTO method is fired by the bottom welcome bar that appears on first login.
finally i solved with a workaround; i don't know if this is the correct way to approach or i am just cheating but i do this way:
first of all some script in the page (i am using bootstrap + jquery)
this approch have the doLogin part called just one time, but the callback is called twice (gapi.client.oauth2.userinfo.get() this function is called twice); with a bit more tweaking with the if / var check i think is possible to call everything once. This way if the user already granted the auth, it will be automatically signed.
I notice that sometimes google have a popup layer on the bottom of layer showing a "welcome back message", but i didn't understand when it appears or if i have to call it manually
I'm facing this same issue here, but I'm calling gapi.auth.signIn() via a button click handler. The callback is still called twice. One thing I noticed between the two authResult objects was that authResult.status.method is 'AUTO' in the first call (before the popup window appears) and is 'PROMPT' in the second call after the window is auto-dismissed due to previous authorisation.
The solution I'm exploring now is to ignore the AUTO instance and only process the PROMPT instance of the callback. Not sure how this will work once I revoke the permissions within Google due to the lack of details in the docs on the 'status' object.
That is the intentional plan for page level config! It being present in the page causes the callback to fire when the Javascript is finished loading. What you should do is prepare for that in your code.
Don't show the sign in button until you have received a callback - if
authResult['status']['signed_in'] == true
, then treat the user as signed in (setup a session etc, whatever you would normally do). If it is false, then display the button.I would avoid using approval prompt force if you can!