Stay organized with collectionsSave and categorize content based on your preferences.
Signing in users with Google
This document shows you how to use Identity Platform to sign in users with
Google.
Before you begin
This tutorial assumes you've already enabled Identity Platform, and have a
basic web app written using HTML and JavaScript. See theQuickstartto learn how.
Configuring Google as a provider
To configure Google as an identity provider:
Go to theIdentity Providerspage in the Google Cloud console.
Enter your GoogleWeb Client IDandWeb Secret. If
you don't already have an ID and secret, you can obtain one from theAPI's & Servicespage.
Configure the URI listed underConfigure Googleas a valid OAuth
redirect URI for your Google app. If you configured a custom domain in Identity Platform,
update the redirect URI in your Google app configuration to use the custom domain instead
of the default domain. For example, changehttps://myproject.firebaseapp.com/__/auth/handlertohttps://auth.myownpersonaldomain.com/__/auth/handler.
Register your app's domains by clickingAdd DomainunderAuthorized Domains. For development purposes,localhostis already
enabled by default.
UnderConfigure your application, clickSetup Details. Copy the
snippet into your app's code to initialize the Identity Platform
client SDK.
Optional:Add OAuth scopes. Scopes specify what data you are
requesting from Google. More sensitive data may require specific
scopes. Consult the provider'sdocumentationto determine what scopes your app needs.
Optional:Localize the authentication flow. You can specify a language,
or use the device's default language:
Web version 9
import{getAuth}from"firebase/auth";constauth=getAuth();auth.languageCode='it';// To apply the default browser preference instead of explicitly setting it.// auth.useDeviceLanguage();
Optional:Specify additional custom OAuth parameters. These are
specific to Google, and are typically used to customize the
authentication experience. You can't pass parameters reserved by OAuth or Identity Platform.
Use the Google provider object to sign in the user. You can either
open a pop-up window, or redirect the current page. Redirecting is easier
for users on mobile devices.
To show a pop-up, callsignInWithPopup():
Web version 9
import{getAuth,signInWithPopup,GoogleAuthProvider}from"firebase/auth";constauth=getAuth();signInWithPopup(auth,provider).then((result)=>{// This gives you a Google Access Token. You can use it to access the Google API.constcredential=GoogleAuthProvider.credentialFromResult(result);consttoken=credential.accessToken;// The signed-in user info.constuser=result.user;// IdP data available using getAdditionalUserInfo(result)// ...}).catch((error)=>{// Handle Errors here.consterrorCode=error.code;consterrorMessage=error.message;// The email of the user's account used.constemail=error.customData.email;// The AuthCredential type that was used.constcredential=GoogleAuthProvider.credentialFromError(error);// ...});
firebase.auth().signInWithPopup(provider).then((result)=>{/** @type {firebase.auth.OAuthCredential} */varcredential=result.credential;// This gives you a Google Access Token. You can use it to access the Google API.vartoken=credential.accessToken;// The signed-in user info.varuser=result.user;// IdP data available in result.additionalUserInfo.profile.// ...}).catch((error)=>{// Handle Errors here.varerrorCode=error.code;varerrorMessage=error.message;// The email of the user's account used.varemail=error.email;// The firebase.auth.AuthCredential type that was used.varcredential=error.credential;// ...});
Then, retrieve the Google token by callinggetRedirectResult()when your page loads:
Web version 9
import{getAuth,getRedirectResult,GoogleAuthProvider}from"firebase/auth";constauth=getAuth();getRedirectResult(auth).then((result)=>{// This gives you a Google Access Token. You can use it to access Google APIs.constcredential=GoogleAuthProvider.credentialFromResult(result);consttoken=credential.accessToken;// The signed-in user info.constuser=result.user;// IdP data available using getAdditionalUserInfo(result)// ...}).catch((error)=>{// Handle Errors here.consterrorCode=error.code;consterrorMessage=error.message;// The email of the user's account used.constemail=error.customData.email;// The AuthCredential type that was used.constcredential=GoogleAuthProvider.credentialFromError(error);// ...});
firebase.auth().getRedirectResult().then((result)=>{if(result.credential){/** @type {firebase.auth.OAuthCredential} */varcredential=result.credential;// This gives you a Google Access Token. You can use it to access the Google API.vartoken=credential.accessToken;// ...}// The signed-in user info.varuser=result.user;// IdP data available in result.additionalUserInfo.profile.// ...}).catch((error)=>{// Handle Errors here.varerrorCode=error.code;varerrorMessage=error.message;// The email of the user's account used.varemail=error.email;// The firebase.auth.AuthCredential type that was used.varcredential=error.credential;// ...});
Use the credential to sign in the user with Identity Platform:
Web version 9
import{getAuth,signInWithCredential}from"firebase/auth";// Sign in with the credential from the user.constauth=getAuth();signInWithCredential(auth,credential).then((result)=>{// Signed in// ...}).catch((error)=>{// Handle Errors here.consterrorCode=error.code;consterrorMessage=error.message;// The email of the user's account used.constemail=error.customData.email;// ...});
// Sign in with the credential from the user.firebase.auth().signInWithCredential(credential).then((result)=>{// Signed in// ...}).catch((error)=>{// Handle Errors here.consterrorCode=error.code;consterrorMessage=error.message;// The email of the user's account used.constemail=error.email;// ...});
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Hard to understand","hardToUnderstand","thumb-down"],["Incorrect information or sample code","incorrectInformationOrSampleCode","thumb-down"],["Missing the information/samples I need","missingTheInformationSamplesINeed","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-04 UTC."],[[["\u003cp\u003eThis guide demonstrates how to enable Google sign-in for users on your web application using Identity Platform.\u003c/p\u003e\n"],["\u003cp\u003eConfiguring Google as a provider involves setting up a Web Client ID and Web Secret, and ensuring that your URI is a valid OAuth redirect URI.\u003c/p\u003e\n"],["\u003cp\u003eYou can sign in users using the client SDK, which supports optional configurations like adding OAuth scopes, localizing the authentication flow, or adding custom OAuth parameters.\u003c/p\u003e\n"],["\u003cp\u003eUsers can be signed in by using either a pop-up window (\u003ccode\u003esignInWithPopup()\u003c/code\u003e) or redirecting the page (\u003ccode\u003esignInWithRedirect()\u003c/code\u003e).\u003c/p\u003e\n"],["\u003cp\u003eAlternatively, users can be signed in manually, by integrating Google authentication, and exchanging the received token for an Identity Platform credential.\u003c/p\u003e\n"]]],[],null,[]]