Firebase Authentication Android provides backend services to developers out there, in order to authenticate users in the application.
In this tutorial, we will implement the Firebase Authentication Android service provided by Firebase using Email and Password SignIn method.
But first we need to connect our Android project with Firebase to start using Firebase services.
Connecting Firebase project to Android app through Android studio Firebase assistant
To connect Firebase through Android Studio assistant you need to sign in to Android studio with your email. To sign in, click the icon at the top right corner in Android studio and SignIn.
Steps to connect Firebase to Android app:
1. Launch Firebase assistant by heading to Tools->Firebase.
2. Click on “Authentication” and select “Authenticate using a custom authentication system”.
3. Click on “Connect to Firebase”.
4. By clicking Connect to Firebase
. You’ll be taken to the Firebase website to choose a Firebase project (Create new Firebase project if you don’t have one).
Here we will select the existing one named FirebaseAuthentication.
5. Click on “Connect”.
Now you will see a message like the one below.
6. Go back Android Studio and click “Add Firebase Authentication to your app” and hit “Accept changes”, so this will add the required dependencies to your project.
7. Open your Firebase project again and go to Authentication section and select Email/Password to enable it.
8. Enable the first option and click save so it will enable the Email/Password signin method.
Congratulations! You have finished connecting and setting up the Firebase Authentication.
Application code:
Create new activity “RegisterActivity” and paste the below code in activity_register.xml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" android:padding="24dp" tools:context=".RegisterActivity"> <ImageView android:id="@+id/imageView" android:layout_width="200dp" android:layout_height="200dp" android:layout_gravity="center" android:layout_marginTop="25dp" android:src="@drawable/firebase_image"/> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_gravity="center" android:layout_marginTop="25dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Register" android:textSize="30sp" android:textStyle="bold" android:textColor="@color/teal_700" android:gravity="center_horizontal" android:layout_marginVertical="16dp"/> <com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_height="wrap_content"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/etRegEmail" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Email" android:inputType="textEmailAddress"/> </com.google.android.material.textfield.TextInputLayout> <com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_height="wrap_content" android:layout_marginTop="12dp"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/etRegPass" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Password" android:inputType="textPassword"/> </com.google.android.material.textfield.TextInputLayout> <com.google.android.material.button.MaterialButton android:id="@+id/btnRegister" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="24dp" android:text="Register"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:gravity="center" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Already registered" android:textSize="16sp"/> <TextView android:id="@+id/tvLoginHere" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:text="Login here" android:textColor="@color/teal_700" android:textStyle="bold" android:textSize="20sp"/> </LinearLayout> </LinearLayout> </LinearLayout> |
Now open RegisterActivity.java and paste the below code in your Java file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
public class RegisterActivity extends AppCompatActivity { TextInputEditText etRegEmail; TextInputEditText etRegPassword; TextView tvLoginHere; Button btnRegister; FirebaseAuth mAuth; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_sign_up); etRegEmail = findViewById(R.id.etRegEmail); etRegPassword = findViewById(R.id.etRegPass); tvLoginHere = findViewById(R.id.tvLoginHere); btnRegister = findViewById(R.id.btnRegister); mAuth = FirebaseAuth.getInstance(); btnRegister.setOnClickListener(view ->{ createUser(); }); tvLoginHere.setOnClickListener(view ->{ startActivity(new Intent(RegisterActivity.this, LoginActivity.class)); }); } private void createUser(){ String email = etRegEmail.getText().toString(); String password = etRegPassword.getText().toString(); if (TextUtils.isEmpty(email)){ etRegEmail.setError("Email cannot be empty"); etRegEmail.requestFocus(); }else if (TextUtils.isEmpty(password)){ etRegPassword.setError("Password cannot be empty"); etRegPassword.requestFocus(); }else{ mAuth.createUserWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()){ Toast.makeText(RegisterActivity.this, "User registered successfully", Toast.LENGTH_SHORT).show(); startActivity(new Intent(RegisterActivity.this, LoginActivity.class)); }else{ Toast.makeText(RegisterActivity.this, "Registration Error: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show(); } } }); } } } |
Create new activity “LoginActivity” and paste below code to activity_login.xml.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
<?xml version="1.0" encoding="utf-8"?> <LinearLayout 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" android:orientation="vertical" android:padding="24dp" tools:context=".LoginActivity"> <ImageView android:id="@+id/imageView" android:layout_width="200dp" android:layout_height="200dp" android:layout_gravity="center" android:layout_marginTop="25dp" android:src="@drawable/firebase_image"/> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:layout_gravity="center" android:layout_marginTop="25dp"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Login" android:textSize="30sp" android:textStyle="bold" android:textColor="@color/teal_700" android:gravity="center_horizontal" android:layout_marginVertical="16dp"/> <com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_height="wrap_content"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/etLoginEmail" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Email" android:inputType="textEmailAddress"/> </com.google.android.material.textfield.TextInputLayout> <com.google.android.material.textfield.TextInputLayout android:layout_width="match_parent" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_height="wrap_content" android:layout_marginTop="12dp"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/etLoginPass" android:layout_width="match_parent" android:layout_height="wrap_content" android:hint="Password" android:inputType="textPassword"/> </com.google.android.material.textfield.TextInputLayout> <com.google.android.material.button.MaterialButton android:id="@+id/btnLogin" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="24dp" android:text="Login"/> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:gravity="center" android:orientation="horizontal"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Not registered yet" android:textSize="16sp"/> <TextView android:id="@+id/tvRegisterHere" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="8dp" android:text="Register here" android:textColor="@color/teal_700" android:textStyle="bold" android:textSize="20sp"/> </LinearLayout> </LinearLayout> </LinearLayout> |
After this open your “LoginActivity.java” and paste the below code in your Java file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
public class LoginActivity extends AppCompatActivity { TextInputEditText etLoginEmail; TextInputEditText etLoginPassword; TextView tvRegisterHere; Button btnLogin; FirebaseAuth mAuth; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); etLoginEmail = findViewById(R.id.etLoginEmail); etLoginPassword = findViewById(R.id.etLoginPass); tvRegisterHere = findViewById(R.id.tvRegisterHere); btnLogin = findViewById(R.id.btnLogin); mAuth = FirebaseAuth.getInstance(); btnLogin.setOnClickListener(view -> { loginUser(); }); tvRegisterHere.setOnClickListener(view ->{ startActivity(new Intent(LoginActivity.this, RegisterActivity.class)); }); } private void loginUser(){ String email = etLoginEmail.getText().toString(); String password = etLoginPassword.getText().toString(); if (TextUtils.isEmpty(email)){ etLoginEmail.setError("Email cannot be empty"); etLoginEmail.requestFocus(); }else if (TextUtils.isEmpty(password)){ etLoginPassword.setError("Password cannot be empty"); etLoginPassword.requestFocus(); }else{ mAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()){ Toast.makeText(LoginActivity.this, "User logged in successfully", Toast.LENGTH_SHORT).show(); startActivity(new Intent(LoginActivity.this, MainActivity.class)); }else{ Toast.makeText(LoginActivity.this, "Log in Error: " + task.getException().getMessage(), Toast.LENGTH_SHORT).show(); } } }); } } } |
Now we have to add a logout button to our MainActivity so the user will have a logout option when logged in.
Open activity_main.xml and paste the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
<?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout 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=".MainActivity"> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Welcome, You are logged in " android:textSize="30sp" android:textStyle="bold" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" /> <Button android:id="@+id/btnLogout" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="16dp" android:text="Logout" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.5" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@+id/textView" /> </androidx.constraintlayout.widget.ConstraintLayout> |
After this we will implement logout option in our MainActivity.java, so paste the below code in your MainActivity.java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
public class MainActivity extends AppCompatActivity { Button btnLogOut; FirebaseAuth mAuth; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btnLogOut = findViewById(R.id.btnLogout); mAuth = FirebaseAuth.getInstance(); btnLogOut.setOnClickListener(view ->{ mAuth.signOut(); startActivity(new Intent(MainActivity.this, LoginActivity.class)); }); } @Override protected void onStart() { super.onStart(); FirebaseUser user = mAuth.getCurrentUser(); if (user == null){ startActivity(new Intent(MainActivity.this, LoginActivity.class)); } } } |
Now the application is ready to use just run your application on emulator or a real device and try to register yourself by using your email id and password.
Nice