Authenticate Using Yahoo with JavaScript | Firebase Authentication
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:
- Add Firebase to your JavaScript project.
- In the Firebase console, go to Security > Authentication.
- In the Sign-in method tab, enable the Yahoo sign-in provider.
- Add the Client ID and Client Secret from that provider's developer console to the provider configuration:
To register a Yahoo OAuth client, follow the Yahoo developer documentation on registering a web application with Yahoo.
Be sure to select the two OpenID Connect API permissions:
profileandemail.When registering apps with these providers, be sure to register the
*.firebaseapp.comdomain for your project as the redirect domain for your app.
- 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:
- Create an instance of an OAuthProvider using the provider ID yahoo.com.
import { OAuthProvider } from "firebase/auth";
const provider = new OAuthProvider('yahoo.com');
- Optional: Specify additional custom OAuth parameters that you want to send with the OAuth request.
provider.setCustomParameters({
// Prompt user to re-authenticate to Yahoo.
prompt: 'login',
// Localize to French.
language: 'fr'
});
Optional: Specify additional OAuth 2.0 scopes beyond
profileandemailthat you want to request from the authentication provider.// Request access to Yahoo Mail API. provider.addScope('mail-r'); // Request read/write access to user contacts. provider.addScope('sdct-w');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: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);
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.The same pattern can be used with
reauthenticateWithPopup/reauthenticateWithRedirectwhich 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, 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:
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.
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:
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.
To sign out a user, call signOut:
import { getAuth, signOut } from "firebase/auth";
const auth = getAuth();
signOut(auth).then(() => {
// Sign-out successful.
}).catch((error) => {
// An error happened.
});