Question

I have deployed Google+ Sign-in Button, now I have to provide Sign-Out Button, before that, I need to know whether the user is still signed in, by which I can then show or hide this button.

I found this documentation: gapi.auth.checkSessionState(sessionParams, callback): https://developers.google.com/+/web/api/javascript?hl=en#gapiauthchecksessionstatesessionparams_callback

Could someone demo how to use it ?

Many thanks.

Was it helpful?

Solution 2

You don't need to use a separate function call to determine the user's signed in state if you've already added the sign-in button. The sign-in button's callback is going to trigger either on sign-in, when the page loads, or any time that the user's signed-in status changes. The page load trigger (immediate mode), will also help to indicate if the user is a Google signed-in user or not.

See monitoring the user's signed in status, which shows the different status fields that you can check (Google signed in, app signed in, or signed out).

OTHER TIPS

gapi.auth2.getAuthInstance().isSignedIn.get()

This returns boolean

From Google dev docs :

If you pass null to sessionParams.session_state, you can check if the user is signed in to Google, whether or not they have previously authorized your app.

So your code will be like this:

gapi.auth.checkSessionState({session_state: null}, function(isUserNotLoggedIn){
    if (isUserNotLoggedIn) {
    // do some stuff
    }
});

This is an example of how to use it:

gapi.auth.checkSessionState({client_id:'99999999999.apps.googleusercontent.com'}, signinCallback);

where client_id is the id of your project on g+ console. and siginCallback is a function i created to check the state.

IT'S VERY IMPORTANT to notice that the official api documentation says:

If the argument is true, the supplied session_state is still valid. If the argument is false, it is no longer valid and the application should perform a new re-authorization flow with immediate set to true to get an up-to-date access token or detect a non-authorized or nonexistent Google sign-in session.

But when i recive the response of checkSessionState i get true value when im signed off and false when im signed in. I Dont know if there was an error on the documentation but you can try it by yourself.

Hope this has been helpful for you. PD: excuses for my awful english

Adapted from Tom Anthony's Blog

One simple way you can use without the use of an API to see if a user is logged into Google + (Or Google/Facebook/Twitter) is to try to access a resource which they would only be able to access if logged in. The code below attempts to load an image resource for which the user needs to be authenticated to access. By default html <img> tags run onload() if an image is successfully returned, but onerror() otherwise.

<img style="display:none;"
onload="function(){document.getElementById('signOutButton').style.visibility = 'hidden';}"
onerror="function(){document.getElementById('signOutButton').style.visibility = 'visible';}"
src="https://plus.google.com/up/?continue=https://www.google.com/intl/en/images/logos/accounts_logo.png&type=st&gpsrc=ogpy0"
/>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top