Introduction
Firebase provides many features as described in my previous post. One of the most useful features is User Authentication. Different types of User Authentication are:
- Email and Password Authentication
- Google Plus Authentication
- Facebook Authentication
- GitHub Authentication.
- Twitter Authentication
- Phone Number Authentication
- Anonymous
- Play Games Authentication.
In this post, we will learn how to implement Firebase Email & Password authentication. To demonstrate how simplified and easy to use Firebase is, we will build a simple login/register (Firebase Authentication) demo. This separates sensitive user credentials from your application data and lets you focus on the user interface and experience for your Application. It is a simple and suitable way of handling Login, Registration, Forgot Password, and so on.
After creating the project, enable Firebase Email & Password authentication by selecting Authentication in the left pane of the Firebase Console and go to Sign-in-Method Tab as in the following Image.
Getting started with Firebase
Read the following to know how to set up the Firebase in your Android project.
Reference
https://androidmads.blogspot.in/2016/10/android-getting-started-with-firebase.html
Steps
I have split this part into 3 steps as in the following.
- Step 1: Creating a New Project with Empty Activity.
- Step 2: Setting up the Firebase Library.
- Step 3: Implementation of Email & Password Authentication with Firebase.
Step 1 - Create a new project with Empty Activity
- Open Android Studio and select Create a new project.
- Name the project as your wish and select Empty activity.
- Click the “finish” button to create a new project in Android Studio.
Step 2 - Setting up the Firebase Library
In this part, we will see how to set up the library for the project.
- Open your project level build.gradle file and add the following lines in dependencies
- {
- …
- classpath 'com.google.gms:google-services:3.1.0'
- …
- }
- Then add the following lines in allprojects in project level build.gradle file.
- allprojects {
- repositories {
- google()
- jcenter()
- maven {
- url "https://maven.google.com"
- }
- }
- }
- Then add the following lines in app level build.gradle file to apply google services to your project.
- dependencies {
- ...
- implementation 'com.google.firebase:firebase-auth:11.8.0'
- }
- apply plugin: 'com.google.gms.google-services'
- Then click “Sync Now” to setup your project.
Step 3 - Implementation of Email & Password Authentication with Firebase
In this part, we will see how to implement Email & Password Authentication with Firebase & in this step, we will see many features of Firebase.
SIGNUP METHOD
In Firebase Authentication, the credits entered by user might have some validation. For Example, Email ID might be in proper email format and Password must have at-least 6 characters. Firebase Provides createUserWithEmailAndPassword() to create New User in Firebase.
-
- btnSignUp.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- final String email = inputEmail.getText().toString();
- final String password = inputPassword.getText().toString();
- try {
-
-
-
- if (password.length() > 0 && email.length() > 0) {
- PD.show();
-
- auth.createUserWithEmailAndPassword(email, password)
- .addOnCompleteListener(RegisterActivity.this, new OnCompleteListener() {
- @Override
- public void onComplete(@NonNull Task task) {
- if (!task.isSuccessful()) {
- Toast.makeText(
- RegisterActivity.this,
- "Authentication Failed",
- Toast.LENGTH_LONG).show();
- } else {
- Intent intent = new Intent(RegisterActivity.this, MainActivity.class);
- startActivity(intent);
- finish();
- }
- PD.dismiss();
- }
- });
- } else {
- Toast.makeText(
- RegisterActivity.this,
- "Fill All Fields",
- Toast.LENGTH_LONG).show();
- }
-
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
SIGNIN METHOD
Firebase provides signInWithEmailAndPassword() method to sign in the user.
-
- btnLogin.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View view) {
- final String email = inputEmail.getText().toString();
- final String password = inputPassword.getText().toString();
- try {
- if (password.length() > 0 && email.length() > 0) {
- PD.show();
-
- auth.signInWithEmailAndPassword(email, password)
- .addOnCompleteListener(LoginActivity.this, new OnCompleteListener() {
- @Override
- public void onComplete(@NonNull Task task) {
- if (!task.isSuccessful()) {
- Toast.makeText(
- LoginActivity.this,
- "Authentication Failed",
- Toast.LENGTH_LONG).show();
- Log.v("error", task.getResult().toString());
- } else {
- Intent intent = new Intent(LoginActivity.this, MainActivity.class);
- startActivity(intent);
- finish();
- }
- PD.dismiss();
- }
- });
- } else {
- Toast.makeText(
- LoginActivity.this,
- "Fill All Fields",
- Toast.LENGTH_LONG).show();
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- });
Summary
In this part, we have learned how to do SIGN-IN & SIGN-UP with Firebase User Email & Password Authentication. In the next article, we will see the additional features or services provided by Firebase.