Monitoring the user's session state
Stay organized with collections
Save and categorize content based on your preferences.
Page Summary
-
Support for the Google Sign-In library is deprecated.
-
The Google Sign-In library optionally uses FedCM APIs, which will become a requirement.
-
You can attach handlers to the initialized Google Sign-In client to check the user's session state.
-
The
attachClickHandlermethod can be used to silently finish sign-in or prompt re-authorization based on the session state.
After the Google Sign-In client has been initialized, you can attach handlers that check various attributes and methods of the client to determine the user's session state. You can use information returned by the client object to help sync your site's user experience across multiple tabs and devices for your user.
The following code demonstrates using the 2.0 client method
attachClickHandler to create a callback that either silently finishes sign-in
for the user, or prompts the user to re-authorize based on the state of the
user's session.
/**
*TheSign-Inclientobject.
*/
varauth2;
/**
*InitializestheSign-Inclient.
*/
varinitClient=function(){
gapi.load('auth2',function(){
/**
*RetrievethesingletonfortheGoogleAuthlibraryandsetupthe
*client.
*/
auth2=gapi.auth2.init({
client_id:'CLIENT_ID.apps.googleusercontent.com'
});
//Attachtheclickhandlertothesign-inbutton
auth2.attachClickHandler('signin-button',{},onSuccess,onFailure);
});
};
/**
*Handlesuccessfulsign-ins.
*/
varonSuccess=function(user){
console.log('Signed in as '+user.getBasicProfile().getName());
};
/**
*Handlesign-infailures.
*/
varonFailure=function(error){
console.log(error);
};