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

หลังจากไคลเอ็นต์ 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);
};