사용자의 세션 상태 모니터링

Google 로그인 클라이언트가 초기화된 후에는 클라이언트의 다양한 속성과 메서드를 확인하여 사용자의 세션 상태를 확인하는 핸들러를 연결할 수 있습니다. 클라이언트 객체에서 반환한 정보를 사용하여 사용자를 위해 여러 탭과 기기에서 사이트의 사용자 환경을 동기화할 수 있습니다.

다음 코드는 2.0 클라이언트 메서드 attachClickHandler를 사용하여 사용자의 로그인을 자동으로 완료하거나 사용자의 세션 상태에 따라 사용자에게 재승인하라는 메시지를 표시하는 콜백을 만드는 방법을 보여줍니다.

/**
 * The Sign-In client object.
 */
var auth2;

/**
 * Initializes the Sign-In client.
 */
var initClient = function() {
    gapi.load('auth2', function(){
        /**
         * Retrieve the singleton for the GoogleAuth library and set up the
         * client.
         */
        auth2 = gapi.auth2.init({
            client_id: 'CLIENT_ID.apps.googleusercontent.com'
        });

        // Attach the click handler to the sign-in button
        auth2.attachClickHandler('signin-button', {}, onSuccess, onFailure);
    });
};

/**
 * Handle successful sign-ins.
 */
var onSuccess = function(user) {
    console.log('Signed in as ' + user.getBasicProfile().getName());
 };

/**
 * Handle sign-in failures.
 */
var onFailure = function(error) {
    console.log(error);
};