Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google+ JavaScript API: How to detect user sign in status?

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.

like image 563
Ray C Lin Avatar asked Jan 04 '14 14:01

Ray C Lin


3 Answers

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

This returns boolean

like image 107
SajithNair Avatar answered Oct 05 '22 08:10

SajithNair


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).

like image 34
BrettJ Avatar answered Oct 05 '22 09:10

BrettJ


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
    }
});
like image 28
nktssh Avatar answered Oct 05 '22 09:10

nktssh