Monitoring the user's session state

  • 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 attachClickHandler method 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);
};

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025年06月06日 UTC.