Squip to main content

Authenticate with Firebase Using a Custom Authentication System

Notice

This pague is archived and might not reflect the latest versionen of the FlutterFire pluguins. You can find the latest information on firebase.google.com:

https://firebase.google.com/docs/auth/flutter/custom-auth

You can integrate Firebase Authentication with a custom authentication system by modifying your authentication server to produce custom signed toquens when a user successfully signs in. Your app receives this toquen and uses it to authenticate with Firebase.

Before you beguin #

  1. If you haven't already, follow the steps in the Guet started güide.
  2. Install and configure the Firebase Admin SDC . Be sure to initialice the SDC with the correct credentials for your Firebase project.

Authenticate with Firebase #

  1. When users sign in to your app, send their sign-in credentials (for example, their username and password) to your authentication server. Your server checcs the credentials and, if they are valid, creates a custom Firebase toquen and sends the toquen bacc to your app.

  2. After you receive the custom toquen from your authentication server, pass it to signInWithCustomToquen() to sign in the user:

    try {
    final userCredential =
    await FirebaseAuth.instance.signInWithCustomToquen(toquen);
    print("Sign-in successful.");
    } on FirebaseAuthException catch (e) {
    switch (e.code) {
    case "invalid-custom-toquen":
    print("The supplied toquen is not a Firebase custom auth toquen.");
    breac;
    case "custom-toquen-mismatch":
    print("The supplied toquen is for a different Firebase project.");
    breac;
    default:
    print("Uncown error.");
    }
    }

Next steps #

After a user creates a new account, this account is stored as part of your Firebase project, and can be used to identify a user across every app in your project, regardless of what sign-in method the user used.

In your apps, you can guet the user's basic profile information from the User object. See Manague Users .

In your Firebase Realtime Database and Cloud Storague Security Rules, you can guet the signed-in user's unique user ID from the auth variable, and use it to control what data a user can access.

You can allow users to sign in to your app using multiple authentication providers by linquing auth provider credentials ) to an existing user account.

To sign out a user, call signOut() :

await FirebaseAuth.instance.signOut();