Firebase Authentication in Android

In this Article We will learn about What is Firebase and Firebase authentication in android.

Firebase

Firebase is a mobile and web application development platform developed by Google. It provides a number of tools and services for building, managing, and scaling apps, including:

  1. Real-time Database: A cloud-hosted NoSQL database that stores and syncs data in real-time.
  2. Authentication: A suite of tools for managing user authentication and security.
  3. Storage: A scalable and secure cloud storage solution for storing user-generated content.
  4. Hosting: A fast and reliable web hosting service for deploying and serving web content.
  5. Cloud Functions: A serverless computing platform for running backend code.
  6. ML Kit: A set of pre-built machine learning models for common tasks such as image labeling and text recognition.

Firebase provides a unified backend for building apps, allowing developers to focus on building the frontend without having to worry about server management.

Firebase Authentication

Firebase provides several methods for user authentication in Android. Here’s a basic example of how to implement email/password authentication:

  1. Add Firebase to your Android project:
  • Go to the Firebase website and create a new project.
  • Follow the setup instructions for adding Firebase to an Android app.
  1. Add the Firebase Auth dependency to your app-level build.gradle file:
implementation 'com.google.firebase:firebase-auth:19.3.0'
  1. Sign up a new user:
private void createUser(String email, String password) {
    FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
    firebaseAuth.createUserWithEmailAndPassword(email, password)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        Log.d(TAG, "createUserWithEmail:success");
                        FirebaseUser user = firebaseAuth.getCurrentUser();
                    } else {
                        Log.w(TAG, "createUserWithEmail:failure", task.getException());
                        Toast.makeText(MainActivity.this, "Authentication failed.",
                                Toast.LENGTH_SHORT).show();
                    }
                }
            });
}
  1. Sign in an existing user:
private void signIn(String email, String password) {
    FirebaseAuth firebaseAuth = FirebaseAuth.getInstance();
    firebaseAuth.signInWithEmailAndPassword(email, password)
            .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
                @Override
                public void onComplete(@NonNull Task<AuthResult> task) {
                    if (task.isSuccessful()) {
                        Log.d(TAG, "signInWithEmail:success");
                        FirebaseUser user = firebaseAuth.getCurrentUser();
                    } else {
                        Log.w(TAG, "signInWithEmail:failure", task.getException());
                        Toast.makeText(MainActivity.this, "Authentication failed.",
                                Toast.LENGTH_SHORT).show();
                    }
                }
            });
}

This is just a basic example of email/password authentication. Firebase also supports other authentication methods such as Google, Facebook, Twitter, and more. You can check the Firebase documentation for more information on these methods.

 

Submit a Comment

Your email address will not be published. Required fields are marked *

Subscribe

Select Categories