उपयोगकर्ता के सेशन की स्थिति पर नज़र रखना

'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);
};