# Authenticate Using Yahoo with JavaScript

You can let your users authenticate with Firebase using OAuth providers like Yahoo by integrating generic OAuth Login into your app using the Firebase SDK to carry out the end to end sign-in flow.

## Before you begin

To sign in users using Yahoo accounts, you must first enable Yahoo as a sign-in provider for your Firebase project:

1. [Add Firebase to your JavaScript project](/content/docs/web/setup/index.html).
2. In the Firebase console, go to **Security** > [**Authentication**](https://console.firebase.google.com/project/_/authentication/).
3. In the **Sign-in method** tab, enable the **Yahoo** sign-in provider.
4. Add the **Client ID** and **Client Secret** from that provider's developer console to the provider configuration:
   1. To register a Yahoo OAuth client, follow the Yahoo developer documentation on [registering a web application with Yahoo](https://developer.yahoo.com/oauth2/guide/openid_connect/getting_started.html).
      
      Be sure to select the two OpenID Connect API permissions: `profile` and `email`.
   2. When registering apps with these providers, be sure to register the `*.firebaseapp.com` domain for your project as the redirect domain for your app.
5. Click **Save**.

## 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 Yahoo accounts is to handle the entire sign-in flow with the Firebase JavaScript SDK.

To handle the sign-in flow with the Firebase JavaScript SDK, follow these steps:

1. Create an instance of an **OAuthProvider** using the provider ID **yahoo.com**.

```javascript
   import { OAuthProvider } from "firebase/auth";

const provider = new OAuthProvider('yahoo.com');
   ```

2. **Optional**: Specify additional custom OAuth parameters that you want to send with the OAuth request.

```javascript
   provider.setCustomParameters({
       // Prompt user to re-authenticate to Yahoo.
       prompt: 'login',
       // Localize to French.
       language: 'fr'
   });
   ```

3. **Optional**: Specify additional OAuth 2.0 scopes beyond `profile` and `email` that you want to request from the authentication provider. 
   
   ```javascript
   // Request access to Yahoo Mail API.
   provider.addScope('mail-r');
   // Request read/write access to user contacts.
   provider.addScope('sdct-w');
   ```

4. Authenticate with Firebase using the OAuth provider object. You can prompt your users to sign in with their Yahoo 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, call `signInWithPopup`:
   
   ```javascript
   import { getAuth, signInWithPopup } from "firebase/auth";

const auth = getAuth();
   signInWithPopup(auth, provider)
     .then((result) => {
         const credential = OAuthProvider.credentialFromResult(result);
         const accessToken = credential.accessToken;
         const idToken = credential.idToken;
     })
     .catch((error) => {
         // Handle error.
     });
   ```
   
   - To sign in by redirecting to the sign-in page, call `signInWithRedirect`:
   
   ```javascript
   firebase.auth().signInWithRedirect(provider);
   ```

5. While the above examples focus on sign-in flows, you also have the ability to link a Yahoo provider to an existing user using `linkWithPopup`/`linkWithRedirect`.

6. The same pattern can be used with `reauthenticateWithPopup`/`reauthenticateWithRedirect` which can be used to retrieve fresh credentials for sensitive operations that require recent login.

## Handling account-exists-with-different-credential Errors

If you enabled the **One account per email address** setting in the [Firebase console](https://console.firebase.google.com/), when a user tries to sign in a to a provider (such as Yahoo) with an email that already exists for another Firebase user's provider (such as Google), the error `auth/account-exists-with-different-credential` is thrown along with an `AuthCredential` object (Yahoo credential).

### Popup mode

If you use `signInWithPopup`, you can handle `auth/account-exists-with-different-credential` errors with code like the following example:

```javascript
import {
  getAuth,
  linkWithCredential,
  signInWithPopup,
  OAuthProvider,
} from "firebase/auth";

try {
    // Step 1: User tries to sign in using Yahoo.
    let result = await signInWithPopup(getAuth(), new OAuthProvider());
} catch (error) {
    // Step 2: User's email already exists.
    if (error.code === "auth/account-exists-with-different-credential") {
        // The pending Yahoo credential.
        let pendingCred = error.credential;
        // ...
    }
}
```

### Advanced: Handle the sign-in flow manually

Firebase Auth does not support the same capability for providers such as Yahoo due to security requirements. Instead, Firebase Auth offers the ability to handle the entire OAuth flow and the authorization code exchange using the OAuth client ID and secret configured in the Firebase Console.

## Authenticate with Firebase in a Chrome extension

If you are building a Chrome extension app, see the [Offscreen Documents guide](/content/docs/auth/web/chrome-extension#use_offscreen_documents/index.html).

## Customizing the redirect domain for Yahoo sign-in

To avoid displaying your subdomain during Yahoo sign-in, set up a custom domain with Firebase Hosting:

```javascript
var config = {
     apiKey: '...',
     authDomain: 'auth.custom.domain.com',
     // other fields...
};
firebase.initializeApp(config);
```

## Next steps

After a user signs in for the first time, a new user account is created and linked to the credentials. This new account is stored as part of your Firebase project.

You can allow users to sign in to your app using multiple authentication providers by [linking auth provider credentials to an existing user account.](/content/docs/auth/web/account-linking/index.html)

To sign out a user, call [`signOut`](/content/docs/reference/js/auth#signout/index.html):

```javascript
import { getAuth, signOut } from "firebase/auth";

const auth = getAuth();
signOut(auth).then(() => {
  // Sign-out successful.
}).catch((error) => {
  // An error happened.
});
```
