AjapaReporter

Home life · Updated

How to Set Up Firebase Authentication

Time it takes15 min
Steps6in the list below
LevelMediumsome care needed
Official sourceFirebase Documentationcheck it for changes

Quick answer The most common way Firebase Authentication fails is skipping the email/password provider in the console, so sign-in returns an error. Enable the provider first, then add the SDK and call the sign-in method.

Enable the Sign-In Method Before Writing Code

Firebase Authentication is disabled by default for every provider. If you try to call signInWithEmailAndPassword without enabling it, you’ll get an error like “auth/operation-not-allowed.” Open the Firebase console, go to your project, and click Authentication in the left menu. Then click the Sign-in method tab and enable the providers you plan to use—Email/Password is the most common for a standard login form.

For a mobile app, you’ll also need to add the platform-specific setup: on Android, add your app’s SHA-1 fingerprint in Project Settings (needed for Google Sign-In and phone auth); on iOS, upload your APNs key for phone auth. For web, no extra setup is needed beyond the provider toggle.

Install the Firebase SDK and Initialize the App

You need the Firebase SDK in your project before any auth call works. The steps differ slightly by platform, but the principle is the same: install the core Firebase package and the auth module.

  1. Create a Firebase project: Go to the Firebase console, click Add project, name it, and accept the default settings. If you already have a project, skip to step 2.
  2. Register your app: In the project overview, click the platform icon (Web, Android, iOS). For web, you’ll get a config object; for Android/iOS, download the google-services.json or GoogleService-Info.plist file and add it to your app.
  3. Install the SDK: For web, run npm install firebase in your project folder. For Android, add the Firebase BoM and the auth dependency to your build.gradle file. For iOS, use CocoaPods with pod 'FirebaseAuth'.
  4. Initialize Firebase: In your code, import the SDK and call initializeApp with your config (web) or rely on the auto-initialization from the config file (mobile). For web, the config object is in Project Settings > Your apps.
  5. Get the auth instance: After initialization, call getAuth() (web) or FirebaseAuth.getInstance() (Android/iOS) to get the auth object you’ll use for all sign-in calls.
  6. Test the connection: Run a simple sign-up with a test email to confirm the SDK is talking to your project. If it fails, check the provider is enabled and the config file is correct.

Menu labels in the console can differ slightly by version, but the Authentication section is always under the left sidebar.

Sign Up a New User with Email and Password

For a basic email/password app, you need two methods: one to create a user, one to sign in an existing user. The sign-up method creates the user in Firebase and automatically signs them in.

Here’s the generic pattern (language-agnostic):

  • Collect the email and password from your form.
  • Call the create-user method (e.g., createUserWithEmailAndPassword).
  • Handle the success by navigating to your main screen.
  • Handle errors like weak password or email already in use.

For the sign-in method, use signInWithEmailAndPassword with the same credentials. If the user doesn’t exist, you’ll get an “auth/user-not-found” error—you can catch that and offer a sign-up link.

Set Up Password Reset and Email Verification

Most apps need password reset. Firebase provides a built-in email for this, but you must enable it in the console under Authentication > Templates. The reset email contains a link that takes the user to a Firebase-hosted page where they can set a new password.

To trigger the reset, call sendPasswordResetEmail with the user’s email. You can also customize the sender name and reply-to address in the template settings.

For email verification, call sendEmailVerification on the current user. This sends an email with a link that verifies the address. You can check the emailVerified property to restrict access to unverified users—a common pattern is to block access until they verify.

Handle Auth State Changes and Sign Out

Firebase keeps the user signed in across app restarts, so you need a listener to know when the auth state changes. On web, use onAuthStateChanged; on mobile, use addAuthStateListener. This listener fires on initial load and whenever the user signs in or out.

In the listener, check if the user is null. If null, show the login screen; if not, show the main app. This is the cleanest way to manage navigation.

To sign out, call signOut() on the auth instance. This clears the local session and triggers the listener with a null user.

Common Mistakes That Break Sign-In

Most setup problems come from a few predictable places. Here’s what to check first when sign-in fails.

  • Provider not enabled: Go to Authentication > Sign-in method and confirm Email/Password is enabled. Also enable Google or other providers if you’re using them.
  • Wrong config file: On mobile, the google-services.json or GoogleService-Info.plist must match the package name/bundle ID. Double-check the file is in the correct folder and the IDs match.
  • Missing SHA-1 fingerprint: For Android with Google Sign-In, add your debug and release SHA-1 keys in Project Settings. Without this, Google sign-in fails with a 12500 error.
  • Not waiting for initialization: On web, call initializeApp before any auth method. On mobile, the SDK auto-initializes, but if you use a custom setup, ensure it runs first.
  • Using the wrong method for the provider: Email/Password uses signInWithEmailAndPassword; Google uses a popup or redirect flow. Mixing them up gives a confusing error.

If you get a network error, check your internet connection and that your domain is authorized in the console (Authentication > Authorized domains). For local development, localhost is allowed by default.

Frequently Asked Questions

Why does sign-in work in the console but not in my app?

The console lets you add users manually, but your app needs the provider enabled. Also check that your app’s config matches the project—a common mistake is using a config from a different Firebase project.

How do I allow users to sign in with Google?

Enable Google in the Sign-in method tab. For web, you need to add your app’s domain to the Authorized domains list. For Android, add your SHA-1 fingerprint; for iOS, add your reversed client ID from the GoogleService-Info.plist. Then call the Google sign-in method in your code.

Can I use phone number authentication?

Yes, but it requires a verified phone number and costs money per verification. For iOS, you must upload an APNs key in the console; for Android, you need the SHA-1 fingerprint. Enable Phone in the Sign-in method tab, then use the phone auth method in your SDK.

Sources: Firebase Documentation. Menus and buttons can differ slightly by version. Spotted something out of date? Tell us.