One Firebase Auth

Firebase Authentication provides an easy way to handle user authentication in your Android applications. By using Firebase Auth with email and password, you can allow users to sign up, sign in, and manage their accounts. Below is a step-by-step explanation of how to implement this in an Android app using Kotlin.

Check out the full features here

Set Up Firebase in Your Android Project

Create a Firebase Project

  1. Go to the Firebase Console.

  2. Click on "Add project" and follow the instructions to create a new project.

Register Your Android App with Firebase

  1. In the Firebase Console, select your project.

  2. Click on the Android icon to add a new Android app.

  3. Follow the instructions to download the google-services.json file and place it in your project's app directory.

Add Firebase SDK to Your Project

  1. Open your build.gradl or build.gradle.kts file (project-level) and add the following line:

plugins {
    id("com.google.gms.google-services") version "latest-version" apply false // check latest version of google services
}
  1. Open your build.gradle or build.gradle.kts file (app-level) and add the following dependencies:

implementation("com.google.firebase:firebase-auth-ktx")
  1. At the bottom of the build.gradle or build.gradle.kts

id("com.google.gms.google-services")

Enable Email/Password Authentication

  1. Go to the Firebase Console.

  2. Select your project.

  3. Navigate to "Authentication" -> "Sign-in method".

  4. Enable "Email/Password" as a sign-in provider.

Implement Email/Password Authentication in Your Android App

Create a class and implement the built in OneFirebaseAuth. For example

class FirebaseAuthDataStore : OneFirebaseAuth, FirebaseAuthRepository {
    override val auth: FirebaseAuth = FirebaseAuth.getInstance()
}

Sample Signin

override suspend fun login(body: FirebaseAuthRequest): Flow<Resource<FirebaseUser>> {
    return callbackFlow {
        signIn(body.email, body.password) { body ->
            trySend(body)
        }

        awaitClose(this::close)
    }
}

signIn is one of the features contained in OneFirebaseAuth to make it easier for you to create login functions.

Sample SignUp

override suspend fun register(body: FirebaseAuthRequest): Flow<Resource<FirebaseUser>> {
    return callbackFlow {
        signUp(body.email, body.password) { body ->
            trySend(body)
        }

        awaitClose(this::close)
    }
}

signUp is one of the features contained in OneFirebaseAuth to make it easier for you to create signup functions. If you want verification email, you can add parameter sendVerification in signUp function. For example:

override suspend fun register(body: FirebaseAuthRequest): Flow<Resource<FirebaseUser>> {
    return callbackFlow {
        signUp(body.email, body.password, sendVerification = true) { body ->
            trySend(body)
        }

        awaitClose(this::close)
    }
}

Sample Reset Password

override suspend fun resetPassword(email: String): Flow<Resource<Boolean>> {
    return callbackFlow {
        resetPassword(email) { body ->
            trySend(body)
        }

        awaitClose(this::close)
    }
}

Sample SignOut

override suspend fun signOut(): Flow<Resource<Boolean>> {
    return callbackFlow {
        signOut { body ->
            trySend(body)
        }

        awaitClose(this::close)
    }
}

Full Code

class FirebaseAuthDataStore : OneFirebaseAuth, FirebaseAuthRepository {

    override val auth: FirebaseAuth = FirebaseAuth.getInstance()

    override suspend fun login(body: FirebaseAuthRequest): Flow<Resource<FirebaseUser>> {
        return callbackFlow {
            signIn(body.email, body.password) { body ->
                trySend(body)
            }

            awaitClose(this::close)
        }
    }

    override suspend fun register(body: FirebaseAuthRequest): Flow<Resource<FirebaseUser>> {
        return callbackFlow {
            signUp(body.email, body.password) { body ->
                trySend(body)
            }

            awaitClose(this::close)
        }
    }

    override suspend fun resetPassword(email: String): Flow<Resource<Boolean>> {
        return callbackFlow {
            resetPassword(email) { body ->
                trySend(body)
            }

            awaitClose(this::close)
        }
    }

    override suspend fun signOut(): Flow<Resource<Boolean>> {
        return callbackFlow {
            signOut { body ->
                trySend(body)
            }

            awaitClose(this::close)
        }
    }

}

Last updated