Mengintegrasikan Login dengan Google menggunakan pemroses
Tetap teratur dengan koleksi
Simpan dan kategorikan konten berdasarkan preferensi Anda.
Pemroses menyediakan cara untuk merespons perubahan secara otomatis dalam sesi Login pengguna saat ini. Misalnya, setelah metode startup menginisialisasi
objek auth2 Login dengan Google, Anda dapat menyiapkan pemroses untuk merespons peristiwa
seperti perubahan status auth2.isSignedIn, atau perubahan pada auth2.currentUser.
Kode berikut menunjukkan penggunaan metode klien 2.0
listen() untuk merespons perubahan pada auth2.isSignedIn dan auth2.currentUser.
varauth2;//TheSign-Inobject.vargoogleUser;//Thecurrentuser./***CallsstartAuthafterSigninV2finishessettingup.*/varappStart=function(){gapi.load('auth2',initSigninV2);};/***InitializesSigninv2andsetsuplisteners.*/varinitSigninV2=function(){auth2=gapi.auth2.init({client_id:'CLIENT_ID.apps.googleusercontent.com',scope:'profile'});//Listenforsign-instatechanges.auth2.isSignedIn.listen(signinChanged);//Listenforchangestocurrentuser.auth2.currentUser.listen(userChanged);//Signintheuseriftheyarecurrentlysignedin.if(auth2.isSignedIn.get()==true){auth2.signIn();}//Startwiththecurrentlivevalues.refreshValues();};/***Listenermethodforsign-outlivevalue.**@param{boolean}valtheupdatedsignedoutstate.*/varsigninChanged=function(val){console.log('Signin state changed to ',val);document.getElementById('signed-in-cell').innerText=val;};/***Listenermethodforwhentheuserchanges.**@param{GoogleUser}usertheupdateduser.*/varuserChanged=function(user){console.log('User now: ',user);googleUser=user;updateGoogleUser();document.getElementById('curr-user-cell').innerText=JSON.stringify(user,undefined,2);};/***UpdatesthepropertiesintheGoogleUsertableusingthecurrentuser.*/varupdateGoogleUser=function(){if(googleUser){document.getElementById('user-id').innerText=googleUser.getId();document.getElementById('user-scopes').innerText=googleUser.getGrantedScopes();document.getElementById('auth-response').innerText=JSON.stringify(googleUser.getAuthResponse(),undefined,2);}else{document.getElementById('user-id').innerText='--';document.getElementById('user-scopes').innerText='--';document.getElementById('auth-response').innerText='--';}};/***RetrievesthecurrentuserandsignedinstatesfromtheGoogleAuth*object.*/varrefreshValues=function(){if(auth2){console.log('Refreshing values...');googleUser=auth2.currentUser.get();document.getElementById('curr-user-cell').innerText=JSON.stringify(googleUser,undefined,2);document.getElementById('signed-in-cell').innerText=auth2.isSignedIn.get();updateGoogleUser();}}
[null,null,["Terakhir diperbarui pada 2025-07-25 UTC."],[[["\u003cp\u003eThe Google Sign-In library optionally uses FedCM APIs, which will become a requirement, necessitating an impact assessment to ensure continued user sign-in functionality.\u003c/p\u003e\n"],["\u003cp\u003eSupport for the Google Sign-In library is now deprecated, and users should consult the Deprecation and Sunset guide for more information.\u003c/p\u003e\n"],["\u003cp\u003eListeners can be used to monitor and respond to changes in a user's sign-in session, such as changes in the \u003ccode\u003eauth2.isSignedIn\u003c/code\u003e state or the \u003ccode\u003eauth2.currentUser\u003c/code\u003e object.\u003c/p\u003e\n"],["\u003cp\u003eThe \u003ccode\u003elisten()\u003c/code\u003e method in the 2.0 client allows developers to receive real-time updates about changes in the user's sign-in status and current user information.\u003c/p\u003e\n"]]],[],null,["# Integrating Google Sign-In using listeners\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\nListeners provide a way to automatically respond to changes in the current\nuser's Sign-In session. For example, after your startup method initializes the\nGoogle Sign-In auth2 object, you can set up listeners to respond to events\nlike `auth2.isSignedIn` state changes, or changes in `auth2.currentUser`.\n\nThe following code demonstrates using the 2.0 client method\n`listen()` to respond to changes in `auth2.isSignedIn` and `auth2.currentUser`. \n\n var auth2; // The Sign-In object.\n var googleUser; // The current user.\n\n\n /**\n * Calls startAuth after Sign in V2 finishes setting up.\n */\n var appStart = function() {\n gapi.load('auth2', initSigninV2);\n };\n\n\n /**\n * Initializes Signin v2 and sets up listeners.\n */\n var initSigninV2 = function() {\n auth2 = gapi.auth2.init({\n client_id: 'CLIENT_ID.apps.googleusercontent.com',\n scope: 'profile'\n });\n\n // Listen for sign-in state changes.\n auth2.isSignedIn.listen(signinChanged);\n\n // Listen for changes to current user.\n auth2.currentUser.listen(userChanged);\n\n // Sign in the user if they are currently signed in.\n if (auth2.isSignedIn.get() == true) {\n auth2.signIn();\n }\n\n // Start with the current live values.\n refreshValues();\n };\n\n\n /**\n * Listener method for sign-out live value.\n *\n * @param {boolean} val the updated signed out state.\n */\n var signinChanged = function (val) {\n console.log('Signin state changed to ', val);\n document.getElementById('signed-in-cell').innerText = val;\n };\n\n\n /**\n * Listener method for when the user changes.\n *\n * @param {GoogleUser} user the updated user.\n */\n var userChanged = function (user) {\n console.log('User now: ', user);\n googleUser = user;\n updateGoogleUser();\n document.getElementById('curr-user-cell').innerText =\n JSON.stringify(user, undefined, 2);\n };\n\n\n /**\n * Updates the properties in the Google User table using the current user.\n */\n var updateGoogleUser = function () {\n if (googleUser) {\n document.getElementById('user-id').innerText = googleUser.getId();\n document.getElementById('user-scopes').innerText =\n googleUser.getGrantedScopes();\n document.getElementById('auth-response').innerText =\n JSON.stringify(googleUser.getAuthResponse(), undefined, 2);\n } else {\n document.getElementById('user-id').innerText = '--';\n document.getElementById('user-scopes').innerText = '--';\n document.getElementById('auth-response').innerText = '--';\n }\n };\n\n\n /**\n * Retrieves the current user and signed in states from the GoogleAuth\n * object.\n */\n var refreshValues = function() {\n if (auth2){\n console.log('Refreshing values...');\n\n googleUser = auth2.currentUser.get();\n\n document.getElementById('curr-user-cell').innerText =\n JSON.stringify(googleUser, undefined, 2);\n document.getElementById('signed-in-cell').innerText =\n auth2.isSignedIn.get();\n\n updateGoogleUser();\n }\n }"]]