// \`googleUser\` from the onsuccess Google Sign In callback.constcredential=GoogleAuthProvider.credential(googleUser.getAuthResponse().id_token);constresult=awaitsignInWithCredential(credential);
GoogleAuthProvider.credentialFromError()
Used to extract the underlying OAuthCredential from a AuthError which was thrown during a sign-in, link, or reauthenticate operation.
// Sign in using a redirect.constprovider=newGoogleAuthProvider();// Start a sign in process for an unauthenticated user.provider.addScope('profile');provider.addScope('email');awaitsignInWithRedirect(auth,provider);// This will trigger a full page redirect away from your app// After returning from the redirect when your app initializes you can obtain the resultconstresult=awaitgetRedirectResult(auth);if(result){// This is the signed-in userconstuser=result.user;// This gives you a Google Access Token.constcredential=GoogleAuthProvider.credentialFromResult(result);consttoken=credential.accessToken;}
Example 2
// Sign in using a popup.constprovider=newGoogleAuthProvider();provider.addScope('profile');provider.addScope('email');constresult=awaitsignInWithPopup(auth,provider);// The signed-in user info.constuser=result.user;// This gives you a Google Access Token.constcredential=GoogleAuthProvider.credentialFromResult(result);consttoken=credential.accessToken;
[[["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 2024-02-01 UTC."],[],[],null,["# GoogleAuthProvider class\n\nProvider for generating an [OAuthCredential](./auth.oauthcredential.md#oauthcredential_class) for [ProviderId](./auth.md#providerid).GOOGLE.\n\n**Signature:** \n\n export declare class GoogleAuthProvider extends BaseOAuthProvider \n\n**Extends:** BaseOAuthProvider\n\nConstructors\n------------\n\n| Constructor | Modifiers | Description |\n|-------------------------------------------------------------------------------|-----------|-------------------------------------------------------------|\n| [(constructor)()](./auth.googleauthprovider.md#googleauthproviderconstructor) | | Constructs a new instance of the `GoogleAuthProvider` class |\n\nProperties\n----------\n\n| Property | Modifiers | Type | Description |\n|-----------------------------------------------------------------------------------------------|-----------|--------------|--------------------------------------------------------------|\n| [GOOGLE_SIGN_IN_METHOD](./auth.googleauthprovider.md#googleauthprovidergoogle_sign_in_method) | `static` | 'google.com' | Always set to [SignInMethod](./auth.md#signinmethod).GOOGLE. |\n| [PROVIDER_ID](./auth.googleauthprovider.md#googleauthproviderprovider_id) | `static` | 'google.com' | Always set to [ProviderId](./auth.md#providerid).GOOGLE. |\n\nMethods\n-------\n\n| Method | Modifiers | Description |\n|-------------------------------------------------------------------------------------------------------------|-----------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [credential(idToken, accessToken)](./auth.googleauthprovider.md#googleauthprovidercredential) | `static` | Creates a credential for Google. At least one of ID token and access token is required. |\n| [credentialFromError(error)](./auth.googleauthprovider.md#googleauthprovidercredentialfromerror) | `static` | Used to extract the underlying [OAuthCredential](./auth.oauthcredential.md#oauthcredential_class) from a [AuthError](./auth.autherror.md#autherror_interface) which was thrown during a sign-in, link, or reauthenticate operation. |\n| [credentialFromResult(userCredential)](./auth.googleauthprovider.md#googleauthprovidercredentialfromresult) | `static` | Used to extract the underlying [OAuthCredential](./auth.oauthcredential.md#oauthcredential_class) from a [UserCredential](./auth.usercredential.md#usercredential_interface). |\n\nGoogleAuthProvider.(constructor)\n--------------------------------\n\nConstructs a new instance of the `GoogleAuthProvider` class\n\n**Signature:** \n\n constructor();\n\nGoogleAuthProvider.GOOGLE_SIGN_IN_METHOD\n----------------------------------------\n\nAlways set to [SignInMethod](./auth.md#signinmethod).GOOGLE.\n\n**Signature:** \n\n static readonly GOOGLE_SIGN_IN_METHOD: 'google.com';\n\nGoogleAuthProvider.PROVIDER_ID\n------------------------------\n\nAlways set to [ProviderId](./auth.md#providerid).GOOGLE.\n\n**Signature:** \n\n static readonly PROVIDER_ID: 'google.com';\n\nGoogleAuthProvider.credential()\n-------------------------------\n\nCreates a credential for Google. At least one of ID token and access token is required.\n\n**Signature:** \n\n static credential(idToken?: string | null, accessToken?: string | null): OAuthCredential;\n\n#### Parameters\n\n| Parameter | Type | Description |\n|-------------|----------------|----------------------|\n| idToken | string \\| null | Google ID token. |\n| accessToken | string \\| null | Google access token. |\n\n**Returns:**\n\n[OAuthCredential](./auth.oauthcredential.md#oauthcredential_class)\n\n### Example\n\n // \\`googleUser\\` from the onsuccess Google Sign In callback.\n const credential = GoogleAuthProvider.credential(googleUser.getAuthResponse().id_token);\n const result = await signInWithCredential(credential);\n\nGoogleAuthProvider.credentialFromError()\n----------------------------------------\n\nUsed to extract the underlying [OAuthCredential](./auth.oauthcredential.md#oauthcredential_class) from a [AuthError](./auth.autherror.md#autherror_interface) which was thrown during a sign-in, link, or reauthenticate operation.\n\n**Signature:** \n\n static credentialFromError(error: FirebaseError): OAuthCredential | null;\n\n#### Parameters\n\n| Parameter | Type | Description |\n|-----------|--------------------------------------------------------------|-------------|\n| error | [FirebaseError](./util.firebaseerror.md#firebaseerror_class) | |\n\n**Returns:**\n\n[OAuthCredential](./auth.oauthcredential.md#oauthcredential_class) \\| null\n\nGoogleAuthProvider.credentialFromResult()\n-----------------------------------------\n\nUsed to extract the underlying [OAuthCredential](./auth.oauthcredential.md#oauthcredential_class) from a [UserCredential](./auth.usercredential.md#usercredential_interface).\n\n**Signature:** \n\n static credentialFromResult(userCredential: UserCredential): OAuthCredential | null;\n\n#### Parameters\n\n| Parameter | Type | Description |\n|----------------|---------------------------------------------------------------------|----------------------|\n| userCredential | [UserCredential](./auth.usercredential.md#usercredential_interface) | The user credential. |\n\n**Returns:**\n\n[OAuthCredential](./auth.oauthcredential.md#oauthcredential_class) \\| null\n\n### Example 1\n\n // Sign in using a redirect.\n const provider = new GoogleAuthProvider();\n // Start a sign in process for an unauthenticated user.\n provider.addScope('profile');\n provider.addScope('email');\n await signInWithRedirect(auth, provider);\n // This will trigger a full page redirect away from your app\n\n // After returning from the redirect when your app initializes you can obtain the result\n const result = await getRedirectResult(auth);\n if (result) {\n // This is the signed-in user\n const user = result.user;\n // This gives you a Google Access Token.\n const credential = GoogleAuthProvider.credentialFromResult(result);\n const token = credential.accessToken;\n }\n\n### Example 2\n\n // Sign in using a popup.\n const provider = new GoogleAuthProvider();\n provider.addScope('profile');\n provider.addScope('email');\n const result = await signInWithPopup(auth, provider);\n\n // The signed-in user info.\n const user = result.user;\n // This gives you a Google Access Token.\n const credential = GoogleAuthProvider.credentialFromResult(result);\n const token = credential.accessToken;"]]