Creates a OAuthCredential from a generic OAuth provider's access token or ID token.
The raw nonce is required when an ID token with a nonce field is provided. The SHA-256 hash of the raw nonce must match the nonce field in the ID token.
// `googleUser` from the onsuccess Google Sign In callback.// Initialize a generate OAuth provider with a `google.com` providerId.constprovider=newOAuthProvider('google.com');constcredential=provider.credential({idToken:googleUser.getAuthResponse().id_token,});constresult=awaitsignInWithCredential(credential);
OAuthProvider.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=newOAuthProvider('google.com');// 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 OAuth Access Token for the provider.constcredential=provider.credentialFromResult(auth,result);consttoken=credential.accessToken;}
Example 2
// Sign in using a popup.constprovider=newOAuthProvider('google.com');provider.addScope('profile');provider.addScope('email');constresult=awaitsignInWithPopup(auth,provider);// The signed-in user info.constuser=result.user;// This gives you a OAuth Access Token for the provider.constcredential=provider.credentialFromResult(auth,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-01-19 UTC."],[],[],null,["# OAuthProvider class\n\nProvider for generating generic [OAuthCredential](./auth.oauthcredential.md#oauthcredential_class).\n\n**Signature:** \n\n export declare class OAuthProvider extends BaseOAuthProvider \n\n**Extends:** BaseOAuthProvider\n\nMethods\n-------\n\n| Method | Modifiers | Description |\n|---------------------------------------------------------------------------------------------------|-----------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| [credential(params)](./auth.oauthprovider.md#oauthprovidercredential) | | Creates a [OAuthCredential](./auth.oauthcredential.md#oauthcredential_class) from a generic OAuth provider's access token or ID token. |\n| [credentialFromError(error)](./auth.oauthprovider.md#oauthprovidercredentialfromerror) | `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| [credentialFromJSON(json)](./auth.oauthprovider.md#oauthprovidercredentialfromjson) | `static` | Creates an [OAuthCredential](./auth.oauthcredential.md#oauthcredential_class) from a JSON string or a plain object. |\n| [credentialFromResult(userCredential)](./auth.oauthprovider.md#oauthprovidercredentialfromresult) | `static` | Used to extract the underlying [OAuthCredential](./auth.oauthcredential.md#oauthcredential_class) from a [UserCredential](./auth.usercredential.md#usercredential_interface). |\n\nOAuthProvider.credential()\n--------------------------\n\nCreates a [OAuthCredential](./auth.oauthcredential.md#oauthcredential_class) from a generic OAuth provider's access token or ID token.\n\nThe raw nonce is required when an ID token with a nonce field is provided. The SHA-256 hash of the raw nonce must match the nonce field in the ID token.\n\n**Signature:** \n\n credential(params: OAuthCredentialOptions): OAuthCredential;\n\n#### Parameters\n\n| Parameter | Type | Description |\n|-----------|---------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------|\n| params | [OAuthCredentialOptions](./auth.oauthcredentialoptions.md#oauthcredentialoptions_interface) | Either the options object containing the ID token, access token and raw nonce or the ID token string. |\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 // Initialize a generate OAuth provider with a `google.com` providerId.\n const provider = new OAuthProvider('google.com');\n const credential = provider.credential({\n idToken: googleUser.getAuthResponse().id_token,\n });\n const result = await signInWithCredential(credential);\n\nOAuthProvider.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\nOAuthProvider.credentialFromJSON()\n----------------------------------\n\nCreates an [OAuthCredential](./auth.oauthcredential.md#oauthcredential_class) from a JSON string or a plain object.\n\n**Signature:** \n\n static credentialFromJSON(json: object | string): OAuthCredential;\n\n#### Parameters\n\n| Parameter | Type | Description |\n|-----------|------------------|---------------------------------|\n| json | object \\| string | A plain object or a JSON string |\n\n**Returns:**\n\n[OAuthCredential](./auth.oauthcredential.md#oauthcredential_class)\n\nOAuthProvider.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 OAuthProvider('google.com');\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 OAuth Access Token for the provider.\n const credential = provider.credentialFromResult(auth, result);\n const token = credential.accessToken;\n }\n\n### Example 2\n\n // Sign in using a popup.\n const provider = new OAuthProvider('google.com');\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 OAuth Access Token for the provider.\n const credential = provider.credentialFromResult(auth, result);\n const token = credential.accessToken;"]]