Thursday 16 March 2017

Registration functionality using firebase authentication service

Tags

Hai friends in this tutorial we will learn how to implement registration system using firebase authentication service,so we will store the registration details like name,mail id ,and password into firebase database

Lets start our work

  • create a new android project in android studio
  • setup the firebase with our android project (if u don't know about please see the below video)


  • add the following dependency in our android project under project structure build.gradle(Module: app) file in module section
 compile 'com.google.firebase:firebase-database:10.0.0'  
 compile 'com.google.firebase:firebase-auth:10.0.0'  
  • then click sync now button (top right side of the gradle file)

uses of above dependency

  • firebase database dependency used to uses the database feature of firebase 
  • firebase database dependency used to uses the authentication feature of firebase 

create User Interface for our Application

  • make our activity_main.xml file to below  

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>  
 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:id="@+id/activity_main"  
   android:layout_width="match_parent"  
   android:layout_height="match_parent"  
   >  
   <EditText  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:inputType="textPersonName"  
     android:hint="name"  
     android:ems="10"  
     android:layout_marginTop="115dp"  
     android:id="@+id/name"  
     android:layout_alignParentTop="true"  
     android:layout_alignParentLeft="true"  
     android:layout_alignParentStart="true" />  
   <EditText  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:inputType="textPersonName"  
     android:hint="password"  
     android:ems="10"  
     android:id="@+id/password"  
     android:layout_centerVertical="true"  
     android:layout_alignParentLeft="true"  
     android:layout_alignParentStart="true" />  
   <Button  
     android:text="Register"
     android:onClick="register"  
     android:layout_width="wrap_content"  
     android:layout_height="wrap_content"  
     android:layout_alignParentBottom="true"  
     android:layout_centerHorizontal="true"  
     android:layout_marginBottom="88dp"  
     android:id="@+id/button2" />  
   <EditText  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:inputType="textPersonName"  
     android:hint="email"  
     android:ems="10"  
     android:layout_marginTop="22dp"  
     android:id="@+id/email"  
     android:layout_below="@+id/name"  
     android:layout_alignParentLeft="true"  
     android:layout_alignParentStart="true" />  
 </RelativeLayout>  
  • finally we get user interface like this

  • then make the mainActivity look like this

MainActivity.java

 package com.example.mahesh.test;  
 import android.app.ProgressDialog;  
 import android.os.Bundle;  
 import android.support.annotation.NonNull;  
 import android.support.v7.app.AppCompatActivity;  
 import android.util.Log;  
 import android.view.View;  
 import android.widget.EditText;  
 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.firebase.auth.AuthResult;  
 import com.google.firebase.auth.FirebaseAuth;  
 import com.google.firebase.auth.FirebaseUser;  
 import com.google.firebase.database.DatabaseReference;  
 import com.google.firebase.database.FirebaseDatabase;  
 public class MainActivity extends AppCompatActivity {  
   private static final String TAG = "MainActivity";  
   private DatabaseReference databaseReference;  
   private FirebaseAuth mAuth;  
   private FirebaseAuth.AuthStateListener mAuthListener;  
   private EditText name,email,password;  
   private String userId;  
   private DatabaseReference userIdRef;  
   private ProgressDialog regDialog;  
   @Override  
   public void onStart() {  
     super.onStart();  
     mAuth.addAuthStateListener(mAuthListener);  
   }  
   @Override  
   protected void onCreate(Bundle savedInstanceState) {  
     super.onCreate(savedInstanceState);  
     setContentView(R.layout.activity_main);  
     name=(EditText)findViewById(R.id.name);  
     email=(EditText)findViewById(R.id.email);  
     password=(EditText)findViewById(R.id.password);  
     regDialog=new ProgressDialog(this);  
     regDialog.setMessage("Registering...");  
     databaseReference= FirebaseDatabase.getInstance().getReference().child("Users");  
     mAuth = FirebaseAuth.getInstance();  
     mAuthListener = new FirebaseAuth.AuthStateListener() {  
       @Override  
       public void onAuthStateChanged(@NonNull FirebaseAuth firebaseAuth) {  
         FirebaseUser user = firebaseAuth.getCurrentUser();  
         if (user != null) {  
           // User is signed in  
           userId=user.getUid();  
           Toast.makeText(MainActivity.this, "Signed In", Toast.LENGTH_SHORT).show();  
         } else {  
           // User is signed out  
           Toast.makeText(MainActivity.this, "Signed out", Toast.LENGTH_SHORT).show();  
         }  
         // ...  
       }  
     };  
 }  
   public void register(View view) {  
     regDialog.show();  
     mAuth.createUserWithEmailAndPassword(email.getText().toString(), password.getText().toString())  
         .addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {  
           @Override  
           public void onComplete(@NonNull Task<AuthResult> task) {  
             if(task.isComplete())  
             {  
               regDialog.dismiss();  
               Toast.makeText(getApplicationContext(),"Registered successfully",Toast.LENGTH_SHORT).show();  
               userIdRef=databaseReference.child(userId);  
               userIdRef.child("name").setValue(name.getText().toString());  
             }  
             regDialog.dismiss();  
           }  
         });  
   }  
 }  
  • you need to enable sign-in providers before working with Firebase Authentication system

to enable sign-in providers please follow below steps

  • click here 
  • it will automatically redirects to firebase console page, there you will find your firebase projects
  • then select your project and click Authentication Tab from the left side bar
  • then click Sign-in Method under Authentication like this

  • it will shows Email/Password disabled defaultly like above
  • then you have to enable that option by clicking
  • then only it will works 

Run our Application

  • Now click run button for testing our application
  • you should get screen like this 
  • now enter name,email,password and then click REGISTER button


  • finally you got "Registered Successfully" Toast Message
  • Now check whether our registered Data stored successfully or not 
  • click here 
  • it will automatically redirects to firebase console page, there you will find your firebase projects
  • then select your project and click Authentication Tab from the left side bar
  • it will shows like this(if you did properly)

  • you will see the our registered mail like above but you couldn't find name on authentication i.e(mahesh) because firebase authentication only stores mail & password details(userId also)
  • so we stored name in database section
  • to see the name click database section
  • under Users directory you will get our name(mahesh) like this


please follow the below video in this video you will find step by step process of this example

Thanks for visiting My Blog if you have any doubts please comment below i will come back with better solution

2 comments

This comment has been removed by a blog administrator.


Emoticon Emoticon