Android Firebase Authentication
Firebase is a Google-provided platform to power your mobile app. Firebase is loaded with core features such as user authentication and cloud messaging. In this tutorial, I will demonstrate the use of the authentication feature in Firebase.
I’ll create an Android app and show you how the Firebase Originals feature is implemented in Android. I walk you through creating the sign up and login functionality. In the signup function, we enter the email address for the user and set the password. We also provide the login option to the registered user. Registered users will be sent to the new functionality. This user functionality includes a dummy text and logout button. Clicking the button will enter the user activity.
Step 1.
Open your Android Studio and Create new Empty Project name it which you want. After that fill all require fields.

Step 2.
Now go to tools option of android studio then select Firebase assistant and choose the option of Authentication. Click on Email and password authentication option. as like below the picture:-

Step 3.
Now connect with Firebase Console.


Step 5.
Now add Firebase dependencies into your android project.

Step 6.
Give internet permission into AndroidManifest.xml file.

Step 7.
Now create new empty activity give the name it Register.

Step 8.
Now copy these below code and paste it in activity_register.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Register">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="150dp"
android:text="Authenticator App"
android:textColor="#3C3604"
android:textSize="25sp" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_below="@id/textView"
android:layout_centerHorizontal="true"
android:text="Create New Account"
android:textColor="#212125"
android:textSize="15sp"/>
<EditText
android:id="@+id/fullName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:background="@android:color/black"
android:layout_below="@id/textView2"
android:ems="10"
android:hint="Full Name"
android:inputType="textPersonName"
android:padding="10dp"
android:textColor="#FFFFFF"
android:textColorHint="#FFFFFF"
android:textSize="14sp"/>
<EditText
android:id="@+id/Email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:ems="10"
android:hint="Email"
android:background="@android:color/black"
android:layout_below="@id/fullName"
android:padding="10dp"
android:textColor="#FFFFFF"
android:textColorHint="#FFFFFF"
android:textSize="14sp"
android:inputType="textEmailAddress"/>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_below="@id/Email"
android:ems="10"
android:inputType="textPassword"
android:hint="Password"
android:background="@android:color/black"
android:padding="10dp"
android:textColor="#FFFFFF"
android:textColorHint="#FFFFFF"
android:textSize="14sp"/>
<EditText
android:id="@+id/phone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_below="@+id/password"
android:ems="10"
android:inputType="textPhonetic"
android:hint="Phone"
android:background="@android:color/black"
android:padding="10dp"
android:textColor="#FFFFFF"
android:textColorHint="#FFFFFF"
android:textSize="14sp"/>
<Button
android:id="@+id/registerBtn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:text="Register"
android:textSize="13sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/phone" />
<TextView
android:id="@+id/createText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_below="@id/phone"
android:layout_centerInParent="true"
android:text="Already Registered ? Login Here"
android:textColor="#0B0B0A"/>
</RelativeLayout>
Step 9.
Now open java folder and select Register.java class and copy paste these code.
package com.androiddevlearn.firebaseauth;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.FirebaseFirestore;
import java.util.HashMap;
import java.util.Map;
public class Register extends AppCompatActivity {
public static final String TAG = "TAG";
EditText mFullName,mEmail,mPassword,mPhone;
Button mRegisterBtn;
TextView mLoginBtn;
FirebaseAuth fAuth;
FirebaseFirestore fStore;
String userID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
mFullName = findViewById(R.id.fullName);
mEmail = findViewById(R.id.Email);
mPassword = findViewById(R.id.password);
mPhone = findViewById(R.id.phone);
mRegisterBtn= findViewById(R.id.registerBtn);
mLoginBtn = findViewById(R.id.createText);
fAuth = FirebaseAuth.getInstance();
fStore = FirebaseFirestore.getInstance();
if(fAuth.getCurrentUser() != null){
startActivity(new Intent(getApplicationContext(),MainActivity.class));
finish();
}
mRegisterBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final String email = mEmail.getText().toString().trim();
String password = mPassword.getText().toString().trim();
final String fullName = mFullName.getText().toString();
final String phone = mPhone.getText().toString();
if(TextUtils.isEmpty(email)){
mEmail.setError("Email is Required.");
return;
}
if(TextUtils.isEmpty(password)){
mPassword.setError("Password is Required.");
return;
}
if(password.length() < 6){
mPassword.setError("Password Must be >= 6 Characters");
return;
}
// register the user in firebase
fAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if(task.isSuccessful()){
// send verification link
FirebaseUser fuser = fAuth.getCurrentUser();
fuser.sendEmailVerification().addOnSuccessListener(new OnSuccessListener() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(Register.this, "Verification Email Has been Sent.", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, "onFailure: Email not sent " + e.getMessage());
}
});
Toast.makeText(Register.this, "User Created.", Toast.LENGTH_SHORT).show();
userID = fAuth.getCurrentUser().getUid();
DocumentReference documentReference = fStore.collection("users").document(userID);
Map<String,Object> user = new HashMap<>();
user.put("fName",fullName);
user.put("email",email);
user.put("phone",phone);
documentReference.set(user).addOnSuccessListener(new OnSuccessListener() {
@Override
public void onSuccess(Void aVoid) {
Log.d(TAG, "onSuccess: user Profile is created for "+ userID);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d(TAG, "onFailure: " + e.toString());
}
});
startActivity(new Intent(getApplicationContext(),MainActivity.class));
}else {
Toast.makeText(Register.this, "Error ! " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
}
}
});
}
});
mLoginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(),Login.class));
}
});
}
}
Step 10.
Create new empty activity name it Login.

Step 11.
Now copy and paste these below code into activity_login.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Login">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="150dp"
android:text="@string/app_name"
android:textColor="#070707"
android:textSize="25sp"/>
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Create New Account"
android:layout_below="@id/textView"
android:layout_centerHorizontal="true"
android:textColor="#1F1F93"
android:textSize="15sp"/>
<EditText
android:id="@+id/Email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_below="@id/textView2"
android:background="@android:color/black"
android:ems="10"
android:hint="Email"
android:inputType="textEmailAddress"
android:padding="10dp"
android:textColor="#FFFFFF"
android:textColorHint="#FFFFFF"
android:textSize="14sp"/>
<EditText
android:id="@+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:background="@android:color/black"
android:layout_below="@id/Email"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
android:padding="10dp"
android:textColor="#FFFFFF"
android:textColorHint="#FFFFFF"
android:textSize="14sp"/>
<Button
android:id="@+id/loginBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:layout_below="@id/password"
android:text="Login"
android:textSize="13sp"/>
<TextView
android:id="@+id/createText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_below="@id/loginBtn"
android:layout_centerHorizontal="true"
android:text="New Here ? Create Account"
android:textColor="#0E0D01"/>
<ProgressBar
android:id="@+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"
android:layout_centerHorizontal="true"
android:layout_below="@id/createText"/>
<TextView
android:id="@+id/forgotPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Forgot Password ?"
android:layout_below="@id/progressBar"
android:layout_centerHorizontal="true"
android:textColor="#121211"
android:textStyle="bold"/>
</RelativeLayout>
Step 12.
Open java folder and select Login.java class and copy paste these below code.
package com.androiddevlearn.firebaseauth;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.OnFailureListener;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
public class Login extends AppCompatActivity {
EditText mEmail,mPassword;
Button mLoginBtn;
TextView mCreateBtn,forgotTextLink;
ProgressBar progressBar;
FirebaseAuth fAuth;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
mEmail = findViewById(R.id.Email);
mPassword = findViewById(R.id.password);
progressBar = findViewById(R.id.progressBar);
fAuth = FirebaseAuth.getInstance();
mLoginBtn = findViewById(R.id.loginBtn);
mCreateBtn = findViewById(R.id.createText);
forgotTextLink = findViewById(R.id.forgotPassword);
mLoginBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String email = mEmail.getText().toString().trim();
String password = mPassword.getText().toString().trim();
if(TextUtils.isEmpty(email)){
mEmail.setError("Email is Required.");
return;
}
if(TextUtils.isEmpty(password)){
mPassword.setError("Password is Required.");
return;
}
if(password.length() < 6){
mPassword.setError("Password Must be >= 6 Characters");
return;
}
progressBar.setVisibility(View.VISIBLE);
// authenticate the user
fAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener() {
@Override
public void onComplete(@NonNull Task task) {
if(task.isSuccessful()){
Toast.makeText(Login.this, "Logged in Successfully", Toast.LENGTH_SHORT).show();
startActivity(new Intent(getApplicationContext(),MainActivity.class));
}else {
Toast.makeText(Login.this, "Error ! " + task.getException().getMessage(), Toast.LENGTH_SHORT).show();
progressBar.setVisibility(View.GONE);
}
}
});
}
});
mCreateBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(getApplicationContext(),Register.class));
}
});
forgotTextLink.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final EditText resetMail = new EditText(v.getContext());
final AlertDialog.Builder passwordResetDialog = new AlertDialog.Builder(v.getContext());
passwordResetDialog.setTitle("Reset Password ?");
passwordResetDialog.setMessage("Enter Your Email To Received Reset Link.");
passwordResetDialog.setView(resetMail);
passwordResetDialog.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// extract the email and send reset link
String mail = resetMail.getText().toString();
fAuth.sendPasswordResetEmail(mail).addOnSuccessListener(new OnSuccessListener() {
@Override
public void onSuccess(Void aVoid) {
Toast.makeText(Login.this, "Reset Link Sent To Your Email.", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(Login.this, "Error ! Reset Link is Not Sent" + e.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
});
passwordResetDialog.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
// close the dialog
}
});
passwordResetDialog.create().show();
}
});
}
}
Step 13.
Now run your Emulator and build your application.


