Android Tutorials
Email and Password Authentication Using Firebase in Android Studio
In this post, we’re going to discuss how to authenticate users with Email and Password Using Firebase in android.
You can use Firebase Authentication to let your users authenticate with Firebase using their email addresses and passwords and to manage your app’s password-based accounts.
Now let’s see how to sign-in users using Email and Password.
- Open Android Studio and Create a Project.
- Now Go to Firebase Console and add Create a Project.
- Add your App to the Firebase.
- Now go to Authentication tab in Firebase console, In Sign-In Method Tab Enable Email/Password sign-in method and Click on Save.
- Now go to Android Studio and Add Firebase SDKto your App.
Table of Contents
In app-level Gradle file
dependencies { implementation platform('com.google.firebase:firebase-bom:28.2.0') implementation 'com.google.firebase:firebase-auth' implementation 'com.google.firebase:firebase-analytics' } apply plugin: 'com.google.gms.google-services'
In project-level Gradle File
dependencies { classpath "com.android.tools.build:gradle:4.1.3" classpath 'com.google.gms:google-services:4.3.8' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files }
Now Create a SignupActivity.
activity_signup.xml:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:app="http://schemas.android.com/apk/res-auto" tools:context=".SignupActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="150dp" android:orientation="vertical" android:background="@drawable/reg_bg" android:id="@+id/email_login_ll"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/welcome" android:textColor="@color/white" android:textSize="30sp" android:textStyle="bold" android:layout_gravity="center" android:layout_marginTop="25dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/sign_up_to_continue" android:textColor="@color/white" android:layout_gravity="center"/> </LinearLayout> <LinearLayout android:id="@+id/details_ll" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="15dp" tools:ignore="RtlSymmetry" android:layout_below="@id/email_login_ll" android:layout_marginStart="15dp" android:layout_marginLeft="15dp" android:layout_marginTop="50dp" android:layout_marginEnd="15dp" android:background="@drawable/rectangle_white" android:elevation="15dp" tools:targetApi="lollipop"> <com.google.android.material.textfield.TextInputLayout android:id="@+id/email_outlinedTextField" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:hint="@string/email"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/email_edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@color/black" android:inputType="textEmailAddress"/> </com.google.android.material.textfield.TextInputLayout> <com.google.android.material.textfield.TextInputLayout android:id="@+id/password__outlinedTextField" android:layout_width="match_parent" android:layout_height="wrap_content" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_marginTop="10dp" android:hint="@string/password" app:endIconMode="password_toggle"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/password_edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@color/black" android:inputType="textPassword"/> </com.google.android.material.textfield.TextInputLayout> <com.google.android.material.button.MaterialButton android:id="@+id/register_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="20dp" app:cornerRadius="10dp" app:elevation="5dp" android:text="@string/register" app:rippleColor="@color/white" app:strokeColor="@color/white"/> <ProgressBar android:id="@+id/register_progress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_gravity="center" android:indeterminateTint="@color/purple_500" android:visibility="gone"/> </LinearLayout> <LinearLayout android:id="@+id/already_member_ll" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_below="@id/details_ll" android:gravity="center" android:layout_marginTop="20dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/already_member" android:textSize="15sp" android:layout_gravity="center" android:layout_margin="2dp" android:textColor="@color/black"/> <TextView android:id="@+id/login_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/login" android:textStyle="bold" android:textColor="@color/purple_700" android:textSize="15sp" android:layout_margin="2dp"/> </LinearLayout> </RelativeLayout>
SignupActivity.java:
package com.example.emailauth; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.Task; import com.google.android.material.button.MaterialButton; import com.google.android.material.textfield.TextInputEditText; import com.google.firebase.auth.AuthResult; import com.google.firebase.auth.FirebaseAuth; public class SignupActivity extends AppCompatActivity { private TextInputEditText userEmail, userPassword; private MaterialButton register_btn; private TextView login_tv; private FirebaseAuth mAuth; private String TAG; private ProgressBar register_progress; String userID; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_signup); userEmail = findViewById(R.id.email_edit_text); userPassword = findViewById(R.id.password_edit_text); register_btn = findViewById(R.id.register_btn); register_progress = findViewById(R.id.register_progress); login_tv = findViewById(R.id.login_tv); login_tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { startActivity(new Intent(SignupActivity.this, LoginActivity.class)); finish(); } }); mAuth = FirebaseAuth.getInstance(); register_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { register_btn.setVisibility(View.GONE); register_progress.setVisibility(View.VISIBLE); final String email = userEmail.getText().toString(); final String password = userPassword.getText().toString(); if (email.isEmpty() || password.isEmpty()) { //display an error message Toast.makeText(SignupActivity.this,"Email and Password Shouldn't be Empty",Toast.LENGTH_LONG).show(); register_btn.setVisibility(View.VISIBLE); register_progress.setVisibility(View.GONE); } else { RegisterUserAccount(email, password); } } }); } private void RegisterUserAccount(String email, String password){ mAuth.createUserWithEmailAndPassword(email,password) .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()){ Toast.makeText(SignupActivity.this,"Account Created Successfully",Toast.LENGTH_SHORT).show(); startActivity(new Intent(SignupActivity.this, LoginActivity.class)); }else { Toast.makeText(SignupActivity.this,"Failed to Create an Account" + task.getException().getMessage(),Toast.LENGTH_SHORT).show(); register_btn.setVisibility(View.VISIBLE); register_progress.setVisibility(View.GONE); } } }); } @Override protected void onStart() { super.onStart(); if (mAuth.getCurrentUser() != null){ finish(); startActivity(new Intent(SignupActivity.this, ProfileActivity.class)); } } }
Now Create a LoginActivity.
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=".LoginActivity"> <LinearLayout android:layout_width="match_parent" android:layout_height="150dp" android:orientation="vertical" android:background="@drawable/reg_bg" android:id="@+id/email_login_ll"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/hello_buddy" android:textColor="@color/white" android:textSize="30sp" android:textStyle="bold" android:layout_gravity="center" android:layout_marginTop="25dp"/> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/login_to_continue" android:textColor="@color/white" android:layout_gravity="center"/> </LinearLayout> <LinearLayout android:id="@+id/details_ll" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="15dp" tools:ignore="RtlSymmetry" android:layout_below="@id/email_login_ll" android:layout_marginStart="15dp" android:layout_marginLeft="15dp" android:layout_marginTop="50dp" android:layout_marginEnd="15dp" android:background="@drawable/rectangle_white" android:elevation="15dp" tools:targetApi="lollipop"> <com.google.android.material.textfield.TextInputLayout android:id="@+id/email_outlinedTextField" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:hint="@string/email"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/email_edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@color/black" android:inputType="textEmailAddress"/> </com.google.android.material.textfield.TextInputLayout> <com.google.android.material.textfield.TextInputLayout android:id="@+id/password__outlinedTextField" android:layout_width="match_parent" android:layout_height="wrap_content" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_marginTop="10dp" android:hint="@string/password" app:endIconMode="password_toggle"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/password_edit_text" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@color/black" android:inputType="textPassword"/> </com.google.android.material.textfield.TextInputLayout> <com.google.android.material.button.MaterialButton android:id="@+id/login_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:layout_marginTop="20dp" app:cornerRadius="10dp" app:elevation="5dp" android:text="@string/login" app:rippleColor="@color/white" app:strokeColor="@color/white"/> <ProgressBar android:id="@+id/login_progress" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="10dp" android:layout_gravity="center" android:indeterminateTint="@color/purple_500" android:visibility="gone"/> </LinearLayout> <LinearLayout android:id="@+id/reset_ll" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal" android:layout_below="@id/details_ll" android:gravity="center" android:layout_marginTop="20dp"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/forgot_password" android:textSize="15sp" android:layout_gravity="center" android:layout_margin="2dp" android:textColor="@color/black"/> <TextView android:id="@+id/reset_tv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/reset" android:textStyle="bold" android:textColor="@color/purple_700" android:textSize="15sp" android:layout_margin="2dp"/> </LinearLayout> </RelativeLayout>
LoginActivity.java:
package com.example.emailauth; import androidx.annotation.NonNull; import androidx.appcompat.app.AppCompatActivity; import android.content.DialogInterface; import android.content.Intent; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.widget.ProgressBar; import android.widget.TextView; import android.widget.Toast; import com.google.android.gms.tasks.OnCompleteListener; import com.google.android.gms.tasks.Task; import com.google.android.material.button.MaterialButton; import com.google.android.material.dialog.MaterialAlertDialogBuilder; import com.google.android.material.textfield.TextInputEditText; import com.google.firebase.auth.AuthResult; import com.google.firebase.auth.FirebaseAuth; public class LoginActivity extends AppCompatActivity { private TextInputEditText userEmail, userPassword; private MaterialButton login_btn; private TextView reset_tv; private FirebaseAuth mAuth; private String TAG; private ProgressBar login_progress; String userID; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_login); userEmail = findViewById(R.id.email_edit_text); userPassword = findViewById(R.id.password_edit_text); login_btn = findViewById(R.id.login_btn); login_progress = findViewById(R.id.login_progress); reset_tv = findViewById(R.id.reset_tv); mAuth = FirebaseAuth.getInstance(); login_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { login_btn.setVisibility(View.GONE); login_progress.setVisibility(View.VISIBLE); final String email = userEmail.getText().toString(); final String password = userPassword.getText().toString(); if (email.isEmpty() || password.isEmpty()) { Toast.makeText(LoginActivity.this,"Please Verify All Field",Toast.LENGTH_SHORT); login_btn.setVisibility(View.VISIBLE); login_progress.setVisibility(View.GONE); } else { signIn(email,password); } } }); reset_tv.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ResetPassword(); } }); } private void signIn(String email, String password){ mAuth.signInWithEmailAndPassword(email,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() { @Override public void onComplete(@NonNull Task<AuthResult> task) { if (task.isSuccessful()){ Toast.makeText(LoginActivity.this,"Login Successfully", Toast.LENGTH_SHORT).show(); startActivity(new Intent(LoginActivity.this,ProfileActivity.class)); } else { Toast.makeText(LoginActivity.this,"Login Failed" + task.getException().getMessage(), Toast.LENGTH_SHORT).show(); login_btn.setVisibility(View.VISIBLE); login_progress.setVisibility(View.GONE); } } }); } private void ResetPassword(){ MaterialAlertDialogBuilder materialAlertDialogBuilder = new MaterialAlertDialogBuilder( LoginActivity.this ); View view = LayoutInflater.from(LoginActivity.this).inflate(R.layout.dialog_edit_text,null); TextInputEditText reg_email = view.findViewById(R.id.reset_email_edit_text); materialAlertDialogBuilder.setTitle("Reset Password") .setMessage("Reset Password Link will be Sent to your Registered Email Address") .setCancelable(true) .setView(view) .setPositiveButton("Reset", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { String regemail = reg_email.getText().toString(); mAuth.sendPasswordResetEmail(regemail); Toast.makeText(LoginActivity.this,"Kindly Please Check your Email", Toast.LENGTH_SHORT).show(); } }).show(); } @Override protected void onStart() { super.onStart(); } }
To Show the details of the Logged-in User Let’s Create ProfileActivty.
activity_profile.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=".ProfileActivity"> <LinearLayout android:id="@+id/details_ll" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="15dp" tools:ignore="RtlSymmetry" android:layout_marginStart="15dp" android:layout_marginLeft="15dp" android:layout_marginTop="50dp" android:layout_marginEnd="15dp" android:background="@drawable/rectangle_white" android:elevation="15dp" tools:targetApi="lollipop"> <com.google.android.material.textfield.TextInputLayout android:id="@+id/useremail_outlinedbox" android:layout_width="match_parent" android:layout_height="wrap_content" style="@style/Widget.MaterialComponents.TextInputLayout.OutlinedBox" android:layout_marginTop="10dp" android:layout_gravity="center" android:hint="@string/email"> <com.google.android.material.textfield.TextInputEditText android:id="@+id/user_email" android:layout_width="match_parent" android:layout_height="wrap_content" android:textColor="@color/black" android:textSize="18sp" android:gravity="center" /> </com.google.android.material.textfield.TextInputLayout> <com.google.android.material.button.MaterialButton android:id="@+id/logout_btn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/logout" android:layout_gravity="center" android:layout_marginTop="20dp" app:cornerRadius="10dp" app:elevation="5dp" app:rippleColor="@color/white" app:strokeColor="@color/white"/> </LinearLayout> </RelativeLayout>
ProfileActivity.java:
package com.example.emailauth; import androidx.appcompat.app.AppCompatActivity; import android.content.Intent; import android.os.Bundle; import android.view.View; import com.google.android.material.button.MaterialButton; import com.google.android.material.textfield.TextInputEditText; import com.google.android.material.textfield.TextInputLayout; import com.google.firebase.auth.FirebaseAuth; import com.google.firebase.auth.FirebaseUser; public class ProfileActivity extends AppCompatActivity { private TextInputLayout useremail_outlinedbox; private TextInputEditText user_email; private FirebaseAuth mAuth; private FirebaseUser currentUser; private MaterialButton logout_btn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_profile); useremail_outlinedbox = findViewById(R.id.useremail_outlinedbox); user_email = findViewById(R.id.user_email); logout_btn = findViewById(R.id.logout_btn); useremail_outlinedbox.setEnabled(false); user_email.setEnabled( false ); mAuth = FirebaseAuth.getInstance(); currentUser = mAuth.getCurrentUser(); user_email.setText(currentUser.getEmail()); logout_btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mAuth.signOut(); startActivity(new Intent(ProfileActivity.this, LoginActivity.class)); finish(); } }); } @Override protected void onStart() { super.onStart(); if (mAuth.getCurrentUser() ==null){ startActivity(new Intent(ProfileActivity.this,LoginActivity.class)); } } }
This blog post got very Long. So remaining will be Continued in the Next Blog Post.