Authenticate with Firebase Using a Custom Authentication System

You can integrate Firebase Authentication with a custom authentication system bymodifying your authentication server to produce custom signed tokens when a usersuccessfully signs in. Your app receives this token and uses it to authenticatewith Firebase.

Before you begin

  1. If you haven't already, follow the steps in theGet started guide.
  2. Install and configure the Firebase Admin SDK.Be sure toinitialize the SDKwith the correct credentials for your Firebase project.

Authenticate with Firebase

  1. When users sign in to your app, send their sign-in credentials (forexample, their username and password) to your authentication server. Yourserver checks the credentials and, if they are valid,creates a custom Firebase tokenand sends the token back to your app.

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

    try{finaluserCredential=awaitFirebaseAuth.instance.signInWithCustomToken(token);print("Sign-in successful.");}onFirebaseAuthExceptioncatch(e){switch(e.code){case"invalid-custom-token":print("The supplied token is not a Firebase custom auth token.");break;case"custom-token-mismatch":print("The supplied token is for a different Firebase project.");break;default:print("Unknown error.");}}

Next steps

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

In your apps, you can get the user's basic profile information from theUser object. SeeManage Users.

In your Firebase Realtime Database and Cloud Storage Security Rules, you canget the signed-in user's unique user ID from theauth variable, and use it tocontrol what data a user can access.

You can allow users to sign in to your app using multiple authenticationproviders bylinking auth provider credentials) to anexisting user account.

To sign out a user, callsignOut():

awaitFirebaseAuth.instance.signOut();

Except as otherwise noted, the content of this page is licensed under theCreative Commons Attribution 4.0 License, and code samples are licensed under theApache 2.0 License. For details, see theGoogle Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2025-10-29 UTC.