Stay organized with collectionsSave and categorize content based on your preferences.
You can let your users authenticate with Firebase using their Google Accounts.
You can either use the Firebase SDK to carry out the Google sign-in flow, or
carry out the sign-in flow manually using the Sign In With Google library and
passing the resulting ID token to Firebase.
On theSign in methodtab, enable theGooglesign-in method
and clickSave.
Handle the sign-in flow with the Firebase SDK
If you are building a web app, the easiest way to authenticate your users
with Firebase using their Google Accounts is to handle the sign-in flow with
the Firebase JavaScript SDK. (If you want to authenticate a user in Node.js
or other non-browser environment, you must handle the sign-in flow manually.)
To handle the sign-in flow with the Firebase JavaScript SDK, follow these
steps:
Optional: To localize the provider's OAuth flow to the user's preferred
language without explicitly passing the relevant custom OAuth parameters, update the language
code on the Auth instance before starting the OAuth flow. For example:
Web
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 provider parameters
that you want to send with the OAuth request. To add a custom parameter, callsetCustomParameterson the initialized provider with an object containing the key
as specified by the OAuth provider documentation and the corresponding value. For example:
Authenticate with Firebase using the Google provider object. You can
prompt your users to sign in with their Google Accounts either by opening a
pop-up window or by redirecting to the sign-in page. The redirect method is
preferred on mobile devices.
To sign in with a pop-up window, callsignInWithPopup:
Web
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, you can also retrieve the Google provider's OAuth token by callinggetRedirectResultwhen your page loads:
Web
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;// ...});
If you enabled theOne account per email addresssetting in theFirebaseconsole,
when a user tries to sign in a to a provider (such as Google) with an email that already
exists for another Firebase user's provider (such as Facebook), the errorauth/account-exists-with-different-credentialis thrown along with anAuthCredentialobject (Google ID token). To complete the sign in to the
intended provider, the user has to sign first to the existing provider (Facebook) and then link to the
formerAuthCredential(Google ID token).
Popup mode
If you usesignInWithPopup, you can handleauth/account-exists-with-different-credentialerrors with code like the following
example:
This error is handled in a similar way in the redirect mode, with the difference that the pending
credential has to be cached between page redirects (for example, using session storage).
Advanced: Handle the sign-in flow manually
You can also authenticate with Firebase using a Google Account by handling
the sign-in flow with the Sign In With Google library:
Integrate Sign In With Google into your app by following theintegration guide.
Be sure to configure Google Sign-In with the Google Client ID generated for your Firebase project. You can find your project's Google Client ID in your Project's Developers ConsoleCredentials page.
In the sign-in result callback, exchange the ID token from the Google auth response for a Firebase credential and use it to authenticate with Firebase:
To authenticate with Firebase in a Node.js application:
Sign in the user with their Google Account and get the user's Google ID
token. You can accomplish this in several ways. For example:
If your app has a browser front end, use Google Sign-In as described
in theHandle the
sign-in flow manuallysection. Get the Google ID token from the auth
response:
After you get the user's Google ID token, use it to build a Credential
object and then sign in the user with the credential:
Web
import{getAuth,signInWithCredential,GoogleAuthProvider}from"firebase/auth";// Build Firebase credential with the Google ID token.constcredential=GoogleAuthProvider.credential(id_token);// Sign in with credential from the Google user.constauth=getAuth();signInWithCredential(auth,credential).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);// ...});
// Build Firebase credential with the Google ID token.varcredential=firebase.auth.GoogleAuthProvider.credential(id_token);// Sign in with credential from the Google user.firebase.auth().signInWithCredential(credential).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;// ...});
Customizing the redirect domain for Google sign-in
On project creation, Firebase will provision a unique subdomain for your project:https://my-app-12345.firebaseapp.com.
This will also be used as the redirect mechanism for OAuth sign in. That domain would need to be
allowed for all supported OAuth providers. However, this means that users may see that
domain while signing in to Google before redirecting back to the application:Continue to: https://my-app-12345.firebaseapp.com.
To avoid displaying your subdomain, you can set up a custom domain withFirebase Hosting:
Follow steps 1 through 3 inSet up your domain forHosting. When you verify
your domain ownership,Hostingprovisions an SSL certificate for your custom domain.
Add your custom domain to the list of authorized domains in theFirebaseconsole:auth.custom.domain.com.
In the Google developer console or OAuth setup page, whitelist the URL of the redirect page,
which will be accessible on your custom domain:https://auth.custom.domain.com/__/auth/handler.
When you initialize the JavaScript library, specify your custom domain with theauthDomainfield:
After a user signs in for the first time, a new user account is created and
linked to the credentials—that is, the user name and password, phone
number, or auth provider information—the user signed in with. This new
account is stored as part of your Firebase project, and can be used to identify
a user across every app in your project, regardless of how the user signs in.
In your apps, the recommended way to know the auth status of your user is to
set an observer on theAuthobject. You can then get the user's
basic profile information from theUserobject. SeeManage Users.
In yourFirebase Realtime DatabaseandCloud StorageSecurity Rules, you can
get the signed-in user's unique user ID from theauthvariable,
and use it to control what data a user can access.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-09-05 UTC."],[],[],null,[]]