Firebase

How to implement Firebase Authentication with biometric authentication?

Unlock the secrets of integrating Firebase Authentication with biometric methods for a super secure, user-friendly app experience. Follow this step-by-step guide for easy implementation!

Developer profile skeleton
a developer thinking

Overview

Setting up biometric login with Firebase is like mixing security and ease of use. It taps into Firebase's strong authentication service and pairs it with fingerprint or facial scanning. You start by configuring Firebase Authentication for your app, be it mobile or web. Then you bring in biometric libraries from either Android's BiometricPrompt or iOS's LocalAuthentication.

Once those pieces are in place, you connect the results of biometric checks to Firebase's auth methods. This blend guarantees a smooth and super secure login by using the device’s own biometrics alongside Firebase's features.

Get a Free No-Code Consultation
Meet with Will, CEO at Bootstrapped to get a Free No-Code Consultation
Book a Call
Will Hawkins
CEO at Bootstrapped

How to implement Firebase Authentication with biometric authentication?

Set Up Firebase Project

Step 1: Head over to the Firebase Console and either create a new project or pick one you already have.

Step 2: Add your Android or iOS app to the Firebase project. Follow the instructions to download the google-services.json (for Android) or GoogleService-Info.plist (for iOS) file and pop it into your project.

Step 3: In the Firebase Console, go to the Authentication section and turn on the methods you want, like Email/Password or Google Sign-In.

Add Firebase SDK to Your Project

Step 1: Open up the build.gradle file for your app-level module and add the Firebase dependencies:

dependencies {
    // Firebase Authentication
    implementation 'com.google.firebase:firebase-auth:latest-version'
}

Step 2: Sync your project to make sure all the dependencies are good to go.

Integrate Biometric Authentication

Step 1: In your app's manifest file, add the permission needed for biometric authentication:

<uses-permission android:name="android.permission.USE_BIOMETRIC" />

Step 2: Add the biometric dependencies to your app's build.gradle file:

dependencies {
    // Biometric Authentication Dependencies
    implementation 'androidx.biometric:biometric:latest-version'
}

Step 3: Sync your project again to make sure everything is integrated properly.

Configure Biometric Prompt

Step 1: In your activity or fragment, import the necessary biometric classes:

import androidx.biometric.BiometricPrompt
import androidx.core.content.ContextCompat

Step 2: Create an instance of BiometricPrompt:

val executor = ContextCompat.getMainExecutor(this)
val biometricPrompt = BiometricPrompt(
    this,
    executor,
    object : BiometricPrompt.AuthenticationCallback() {
        override fun onAuthenticationError(errorCode: Int, errString: CharSequence) {
            super.onAuthenticationError(errorCode, errString)
            // Handle error
        }

        override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
            super.onAuthenticationSucceeded(result)
            // Handle success
            authenticateWithFirebase()
        }

        override fun onAuthenticationFailed() {
            super.onAuthenticationFailed()
            // Handle failure
        }
    })

Step 3: Set up the Biometric Prompt info:

val promptInfo = BiometricPrompt.PromptInfo.Builder()
    .setTitle("Biometric Authentication")
    .setSubtitle("Log in using your biometric credential")
    .setNegativeButtonText("Cancel")
    .build()

Step 4: Show the biometric prompt:

biometricPrompt.authenticate(promptInfo)

Firebase Authentication after Biometric Success

Step 1: Implement the authenticateWithFirebase function, which will be called when biometric authentication is successful:

private fun authenticateWithFirebase() {
    val user = FirebaseAuth.getInstance().currentUser
    user?.let {
        // User already authenticated with Firebase
        // Proceed with your app-specific logic
    } ?: run {
        // Handle the case where the user is not logged in to Firebase
        // For first-time login, you may need to sign in with the chosen method (e.g., email, Google sign-in)
    }
}

Step 2: If it's the first-time login, implement the logic for Firebase authentication, like using Firebase email/password or any other configured provider:

private fun firebaseSignIn(email: String, password: String) {
    FirebaseAuth.getInstance().signInWithEmailAndPassword(email, password)
        .addOnCompleteListener(this) { task ->
            if (task.isSuccessful) {
                // Sign-in succeeded
            } else {
                // Sign-in failed
            }
        }
}

Combine Both Authentication Mechanisms

Step 1: Use Firebase to sign in a user once biometric authentication is verified.

Step 2: Store user credentials securely, like with encryption, and use them for Firebase sign-in once biometric verification is complete.

override fun onAuthenticationSucceeded(result: BiometricPrompt.AuthenticationResult) {
    super.onAuthenticationSucceeded(result)
    // Retrieve securely stored credentials
    val email = retrieveEmail()
    val password = retrievePassword()
    // Sign in with Firebase
    firebaseSignIn(email, password)
}

By following these steps, you can combine Firebase Authentication with biometric authentication to give your users a secure and smooth experience.

Explore more Firebase tutorials

Complete Guide to Firebase: Tutorials, Tips, and Best Practices

Explore our Firebase tutorials directory - an essential resource for learning how to create, deploy and manage robust server-side applications with ease and efficiency.

Why are companies choosing Bootstrapped?

40-60%

Faster with no-code

Nocode tools allow us to develop and deploy your new application 40-60% faster than regular app development methods.

90 days

From idea to MVP

Save time, money, and energy with an optimized hiring process. Access a pool of experts who are sourced, vetted, and matched to meet your precise requirements.

1 283 apps

built by our developers

With the Bootstrapped platform, managing projects and developers has never been easier.

hero graphic

Our capabilities

Bootstrapped offers a comprehensive suite of capabilities tailored for startups. Our expertise spans web and mobile app development, utilizing the latest technologies to ensure high performance and scalability. The team excels in creating intuitive user interfaces and seamless user experiences. We employ agile methodologies for flexible and efficient project management, ensuring timely delivery and adaptability to changing requirements. Additionally, Bootstrapped provides continuous support and maintenance, helping startups grow and evolve their digital products. Our services are designed to be affordable and high-quality, making them an ideal partner for new ventures.

Engineered for you

1

Fast Development: Bootstrapped specializes in helping startup founders build web and mobile apps quickly, ensuring a fast go-to-market strategy.

2

Tailored Solutions: The company offers customized app development, adapting to specific business needs and goals, which ensures your app stands out in the competitive market.

3

Expert Team: With a team of experienced developers and designers, Bootstrapped ensures high-quality, reliable, and scalable app solutions.

4

Affordable Pricing: Ideal for startups, Bootstrapped offers cost-effective development services without compromising on quality.

5

Supportive Partnership: Beyond development, Bootstrapped provides ongoing support and consultation, fostering long-term success for your startup.

6

Agile Methodology: Utilizing agile development practices, Bootstrapped ensures flexibility, iterative progress, and swift adaptation to changes, enhancing project success.

Yes, if you can dream it, we can build it.