Google 로그인 클라이언트가 초기화된 후에는 클라이언트의 다양한 속성과 메서드를 확인하는 핸들러를 연결하여 사용자의 세션 상태를 확인할 수 있습니다. 클라이언트 객체에서 반환된 정보를 사용하여 사용자의 여러 탭과 기기에서 사이트의 사용자 환경을 동기화할 수 있습니다.
다음 코드는 2.0 클라이언트 메서드 attachClickHandler를 사용하여 사용자의 로그인을 자동으로 완료하거나 사용자의 세션 상태에 따라 사용자에게 재승인 메시지를 표시하는 콜백을 만드는 방법을 보여줍니다.
/***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-inbuttonauth2.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);};
[null,null,["최종 업데이트: 2025-07-25(UTC)"],[[["\u003cp\u003eThe Google Sign-In JavaScript library is deprecated and will eventually be sunset; developers should consult the Deprecation and Sunset guide for migration details and timelines.\u003c/p\u003e\n"],["\u003cp\u003eGoogle Sign-In will require the use of FedCM APIs in the future, so developers should conduct an impact assessment to ensure their sign-in functionality remains unaffected.\u003c/p\u003e\n"],["\u003cp\u003eThis page provides code demonstrating how to initialize the Google Sign-In client and attach click handlers to manage user sign-in and session state.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can utilize the \u003ccode\u003eauth2\u003c/code\u003e object to handle successful and failed sign-in attempts, accessing user information upon successful authentication.\u003c/p\u003e\n"]]],[],null,["# Monitoring the user's session state\n\n| **Warning:** The Google Sign-In library optionally uses FedCM APIs, and their use will become a requirement. [Conduct an impact assessment](/identity/sign-in/web/gsi-with-fedcm) to confirm that user sign-in continues to function as expected. \n|\n| Support for the Google Sign-In library is deprecated, see the [Deprecation and Sunset](/identity/sign-in/web/deprecation-and-sunset) guide for more.\n\nAfter the Google Sign-In client has been initialized, you can attach\nhandlers that check various attributes and methods of the client to determine\nthe user's session state. You can use information returned by the client object\nto help sync your site's user experience across multiple tabs and devices for\nyour user.\n\nThe following code demonstrates using the 2.0 client method\n`attachClickHandler` to create a callback that either silently finishes sign-in\nfor the user, or prompts the user to re-authorize based on the state of the\nuser's session. \n\n /**\n * The Sign-In client object.\n */\n var auth2;\n\n /**\n * Initializes the Sign-In client.\n */\n var initClient = function() {\n gapi.load('auth2', function(){\n /**\n * Retrieve the singleton for the GoogleAuth library and set up the\n * client.\n */\n auth2 = gapi.auth2.init({\n client_id: 'CLIENT_ID.apps.googleusercontent.com'\n });\n\n // Attach the click handler to the sign-in button\n auth2.attachClickHandler('signin-button', {}, onSuccess, onFailure);\n });\n };\n\n /**\n * Handle successful sign-ins.\n */\n var onSuccess = function(user) {\n console.log('Signed in as ' + user.getBasicProfile().getName());\n };\n\n /**\n * Handle sign-in failures.\n */\n var onFailure = function(error) {\n console.log(error);\n };"]]