การตรวจสอบสถานะเซสชันของผู้ใช้

หลังจากเริ่มต้นไคลเอ็นต์ Google Sign-In แล้ว คุณสามารถแนบตัวแฮนเดิลที่ตรวจสอบแอตทริบิวต์และเมธอดต่างๆ ของไคลเอ็นต์เพื่อระบุสถานะเซสชันของผู้ใช้ คุณสามารถใช้ข้อมูลที่ออบเจ็กต์ไคลเอ็นต์แสดงผลเพื่อช่วยซิงค์ประสบการณ์ของผู้ใช้เว็บไซต์ในแท็บและอุปกรณ์หลายรายการให้กับผู้ใช้

โค้ดต่อไปนี้แสดงการใช้เมธอดไคลเอ็นต์ 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);
};