Connect your app to Firebase
If you haven't already, add Firebase to your Android project .
Add Firebase Authentication to your app
-
In your module (app-level) Gradle file (usually
<project>/<app-module>/build.gradle.kts
or<project>/<app-module>/build.gradle
), add the dependency for the Firebase Authentication library for Android. We recommend using the Firebase Android BoM to control library versioning.dependencies { // Import the BoM for the Firebase platform implementation ( platform ( "com.google.firebase:firebase-bom:34.2.0" )) // Add the dependency for the Firebase Authentication library // When using the BoM , you don't specify versions in Firebase library dependencies implementation ( "com.google.firebase:firebase-auth" ) }
By using the Firebase Android BoM , your app will always use compatible versions of Firebase Android libraries.
(Alternative) Add Firebase library dependencies without using the BoM
If you choose not to use the Firebase BoM , you must specify each Firebase library version in its dependency line.
Note that if you use multiple Firebase libraries in your app, we strongly recommend using the BoM to manage library versions, which ensures that all versions are compatible.
dependencies { // Add the dependency for the Firebase Authentication library // When NOT using the BoM , you must specify versions in Firebase library dependencies implementation ( "com.google.firebase:firebase-auth:24.0.1" ) }
-
To use an authentication provider, you need to enable it in the Firebase console . Go to the Sign-in Method page in the Firebase Authentication section to enable Email/Password sign-in and any other identity providers you want for your app.
(Optional) Prototype and test with Firebase Local Emulator Suite
Before talking about how your app authenticates users, let's introduce a set of tools you can use to prototype and test Authentication functionality: Firebase Local Emulator Suite . If you're deciding among authentication techniques and providers, trying out different data models with public and private data using Authentication and Firebase Security Rules , or prototyping sign-in UI designs, being able to work locally without deploying live services can be a great idea.
An Authentication emulator is part of the Local Emulator Suite , which enables your app to interact with emulated database content and config, as well as optionally your emulated project resources (functions, other databases, and security rules).
Using the Authentication emulator involves just a few steps:
- Adding a line of code to your app's test config to connect to the emulator.
- From the root of your local project directory, running
firebase emulators:start
. - Using the Local Emulator Suite UI for interactive prototyping, or the Authentication emulator REST API for non-interactive testing.
A detailed guide is available at Connect your app to the Authentication emulator . For more information, see the Local Emulator Suite introduction .
Now let's continue with how to authenticate users.
Check current auth state
-
Declare an instance of
FirebaseAuth
.Kotlin
private lateinit var auth : FirebaseAuth . kt