Firebase SDK: Authentication using email/password FIREBASE

Firebase SDK: Authentication using email/password  

Firebase SDK: Authentication using email/password

Firebase SDK: Authentication using email/password

Firebase SDK is slightly the same as Firebase UI. Let's see the steps for implementing email/password authentication using Firebase SDK.

Step1:

In the first step, you have to perform the preliminary steps, which are as follows:

  1. Creating an Android project.
  2. Creating a Firebase project.
  3. Adding Firebase to the Android project or application, either manually or Firebase Assistance.
  4. Adding the required libraries and JSON files.
  5. Enable email/password sign-in method in Firebase console.

Step 2:

After performing the preliminary steps, let's create a register, login, and sign-out activities.

Step 3:

Now code for register activity. The code is as follows:

Register 

  1. public class MainActivity extends AppCompatActivity {  
  2.   
  3. //Initialization section  
  4. EditText email,pass,username;  
  5. ProgressBar progressBar;  
  6. String userid,Email;  
  7. FirebaseAuth auth;  //FirebaseAuth Instance  
  8.     @Override  
  9.     protected void onCreate(Bundle savedInstanceState) {  
  10.         super.onCreate(savedInstanceState);  
  11.         setContentView(R.layout.activity_main);  
  12.         email=(EditText)findViewById(R.id.email);  
  13.         pass=(EditText)findViewById(R.id.password);  
  14.         username=(EditText)findViewById(R.id.username);  
  15.         progressBar=(ProgressBar)findViewById(R.id.progressBar);  
  16.         auth=FirebaseAuth.getInstance();    //Getting instance of FirebaseAuth  
  17.     }  
  18.     // Checking the current all state  
  19.     @Override  
  20.     public void onStart() {  
  21.         super.onStart();  
  22.   
  23.         // if user logged in, go to sign-in screen  
  24.         if (auth.getCurrentUser() != null) {  
  25.             startActivity(new Intent(this, Sign_out.class));  
  26.             finish();  
  27.         }  
  28.     }  
  29.     @Override  
  30.     protected void onResume(){  
  31.         super.onResume();  
  32.         progressBar.setVisibility(View.GONE);  
  33.     }  
  34.     // Register button click   
  35.     public void onRegisterClicked(View view) {  
  36.   
  37.         //Fetching data  
  38.         String emailInput = email.getText().toString().trim();  
  39.         String password = pass.getText().toString().trim();  
  40.         final String user = username.getText().toString().trim();  
  41.   
  42.         //Validation check   
  43.         if (TextUtils.isEmpty(user)) {  
  44.             Toast.makeText(getApplicationContext(), "Enter username!", Toast.LENGTH_SHORT).show();  
  45.             return;  
  46.         }  
  47.         if (TextUtils.isEmpty(emailInput)) {  
  48.             Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();  
  49.             return;  
  50.         }  
  51.         if (TextUtils.isEmpty(password)) {  
  52.             Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();  
  53.             return;  
  54.         }  
  55.         if (password.length() < 6) {  
  56.             Toast.makeText(getApplicationContext(), "Password too short, enter minimum 6 characters!", Toast.LENGTH_SHORT).show();  
  57.             return;  
  58.         }  
  59.         progressBar.setVisibility(View.VISIBLE);  
  60.   
  61.         //create user with email/password by adding complete listener  
  62.         auth.createUserWithEmailAndPassword(emailInput, password)  
  63.                 .addOnCompleteListener(MainActivity.this, new OnCompleteListener<AuthResult>() {  
  64.                     @Override  
  65.                     public void onComplete(@NonNull Task<AuthResult> task) {  
  66.                         Toast.makeText(MainActivity.this, "createUserWithEmail:onComplete:" + task.isSuccessful(), Toast.LENGTH_SHORT).show();  
  67.                         progressBar.setVisibility(View.GONE);  
  68.   
  69.                         // If sign-in fails, display a message to the user. If sign-in succeeds  
  70.                         // the auth state listener will be notified and logic to handle the  
  71.                         // signed in user can be handled in the listener.  
  72.                         if (!task.isSuccessful()) {  
  73.                             Toast.makeText(MainActivity.this, "Authentication failed." + task.getException(),  
  74.                                     Toast.LENGTH_LONG).show();  
  75.                             Log.e("MyTag", task.getException().toString());  
  76.                         } else {  
  77.   
  78.                             startActivity(new Intent(MainActivity.this, Sign_out.class));  
  79.                             finish();  
  80.                         }  
  81.                     }  
  82.                 });  
  83.     }  
  84.     //Login button click   
  85.     public void onLoginClicked(View view) {  
  86.         startActivity(new Intent(this, login.class));  
  87.     }  
  88. }  

 

Step 4:

After that, You need to code for login and sign-out activity, respectively. The codes are as follows

Login

  1. public class login extends AppCompatActivity {  
  2.     //Declaration section  
  3.     EditText email, password;  
  4.     ProgressBar progressBar;  
  5.     FirebaseAuth auth;  
  6.   
  7.     @Override  
  8.     protected void onCreate(Bundle savedInstanceState) {  
  9.         super.onCreate(savedInstanceState);  
  10.         setContentView(R.layout.activity_login);  
  11.         email = (EditText) findViewById(R.id.email);  
  12.         password = (EditText) findViewById(R.id.password);  
  13.         progressBar = (ProgressBar) findViewById(R.id.progressBar);  
  14.         auth = FirebaseAuth.getInstance();  //Getting instances of FirebaseAuth   
  15.     }  
  16.     //Checking current user is logging or not   
  17.     @Override  
  18.     public void onStart() {  
  19.         super.onStart();  
  20.         // If user logged in, go to sign-in screen  
  21.         if (auth.getCurrentUser() != null) {  
  22.             startActivity(new Intent(this, Sign_out.class));  
  23.             finish();  
  24.         }  
  25.     }  
  26.   
  27.     @Override  
  28.     protected void onResume() {  
  29.         super.onResume();  
  30.         progressBar.setVisibility(View.GONE);  
  31.     }  
  32.     //Login button click  
  33.     public void loginButtonClicked(View view) {  
  34.         String Email = email.getText().toString();  
  35.         final String pass = password.getText().toString();  
  36.           
  37.         //Validation section  
  38.         if (TextUtils.isEmpty(Email)) {  
  39.             Toast.makeText(getApplicationContext(), "Enter email address!", Toast.LENGTH_SHORT).show();  
  40.             return;  
  41.         }  
  42.         if (TextUtils.isEmpty(pass)) {  
  43.             Toast.makeText(getApplicationContext(), "Enter password!", Toast.LENGTH_SHORT).show();  
  44.             return;  
  45.         }  
  46.         progressBar.setVisibility(View.VISIBLE);  
  47.         if (password.length() < 6) {  
  48.             password.setError("Should be greater than 6");  
  49.         }  
  50.         //authenticate user with email/password by adding complete listener   
  51.         auth.signInWithEmailAndPassword(Email, pass)  
  52.                 .addOnCompleteListener(login.this, new OnCompleteListener<AuthResult>() {  
  53.                     @Override  
  54.                     public void onComplete(@NonNull Task<AuthResult> task) {  
  55.                         if (!task.isSuccessful()) {  
  56.                             // there was an error  
  57.                             Toast.makeText(login.this, "Authentication failed." + task.getException(),  
  58.                                     Toast.LENGTH_LONG).show();  
  59.                             Log.e("MyTag", task.getException().toString());  
  60.   
  61.                         } else {  
  62.                             Intent intent = new Intent(login.this, Sign_out.class);  
  63.                             startActivity(intent);  
  64.                             finish();  
  65.                         }  
  66.                     }  
  67.                 });  
  68.     }  
  69. }  

Sign-out

  1. public class Sign_out extends AppCompatActivity {  
  2.   
  3.     @Override  
  4.     protected void onCreate(Bundle savedInstanceState) {  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(R.layout.activity_sign_out);  
  7.     }  
  8.     public void onLogout(View view){  
  9.         FirebaseAuth.getInstance().signOut();  
  10.         startActivity(new Intent(this,MainActivity.class));  
  11.     }  
  12. }
 

Output:

 

 

#askProgrammers
Learn Programming for Free


Join Programmers Community on Telegram


Talk with Experienced Programmers


Just drop a message, we will solve your queries