Email and Password Authentication Using Firebase in Android Studio | Java

Golap Gunjan Barman
The Startup
Published in
4 min readFeb 8, 2021

In this blog post, we’re going to discuss how to authenticate users using their Email and Password with the help of Firebase in android.

Earlier we discussed how to authenticate users using the Google Sing-In method with the help of Firebase in Android. Click here to check.

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.

Overview

Before you begin

  • If you haven’t already, add Firebase to your Android project.
  • If you haven’t yet connected your app to your Firebase project, do so from the Firebase console.
  • Enable Email/Password sign-in:
  • In the Firebase console, open the Auth section.
  • On the Sign-in method tab, enable the Email/password sign-in method and click Save.
  • Declare the dependency for the Firebase Authentication Android library in your module (app-level) Gradle file (usually app/build.gradle).

dependencies {
implementation ‘com.google.firebase:firebase-analytics:<latest_version>’
implementation ‘com.google.firebase:firebase-auth: <latest_version>’

}

Create a password-based account

To create a new user account with a password, complete the following steps in your app’s sign-in activity:

  • In your sign-up activity’s onCreate method, get the shared instance of the FirebaseAuth object:

private FirebaseAuth mAuth; mAuth = FirebaseAuth.getInstance();

  • When initializing your Activity, check to see if the user is currently signed in:

@Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
if(currentUser != null){
reload();
}
}

  • When a new user signs up using your app’s sign-up form, complete any new account validation steps that your app requires, such as verifying that the new account’s password was correctly typed and meets your complexity requirements.
  • Create a new account by passing the new user’s email address and password to createUserWithEmailAndPassword(..):

mAuth.createUserWithEmailAndPassword(email, password)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {

// user account created successfully
showMessage(“Account created”);
userID = mAuth.getCurrentUser().getUid();
DocumentReference documentReference = fStore.collection(“user_profile”).document(userID);
Map<String, Object> user = new HashMap<>();
user.put(“Name”, name);
user.put(“Email”, email);
user.put(“Password”, password);
documentReference.set(user).addOnSuccessListener(new OnSuccessListener<Void>() {

@Override
public void onSuccess(Void aVoid) {
Log.
d(TAG, “onSuccess: user Profile is created for “ + userID);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.
d(TAG, “onFailure: “ + e.toString());
}
});
// after we created user account we need to update his profile picture and name
//check user photo is picked or not
if (pickedImgUri != null){
updateUserInfo(name, pickedImgUri, mAuth.getCurrentUser());

}
else {
updateUserInfoWithoutPhoto(name,mAuth.getCurrentUser());
}

} else {

// account creation failed
showMessage(“account creation failed” + task.getException().getMessage());
regBtn.setVisibility(View.
VISIBLE);
loadingProgress.setVisibility(View.
INVISIBLE);

}
}
});

  • If the new account was created, the user is also signed in. In the callback, you can use the getCurrentUser method to get the user’s account data.

Sign in a user with an email address and password

The steps for signing in as a user with a password are similar to the steps for creating a new account. In your app’s sign-in activity, do the following:

  • In your sign-in activity’s onCreate method, get the shared instance of the FirebaseAuth object:

private FirebaseAuth mAuth;

mAuth = FirebaseAuth.getInstance();

  • When initializing your Activity, check to see if the user is currently signed in:

@Override
public void onStart() {
super.onStart();
// Check if user is signed in (non-null) and update UI accordingly.
FirebaseUser currentUser = mAuth.getCurrentUser();
if(currentUser != null){
reload();
}
}

  • When a user signs in to your app, pass the user’s email address and password to signInWithEmailAndPassword(..):

mAuth.signInWithEmailAndPassword(mail,password).addOnCompleteListener(new OnCompleteListener<AuthResult>() {

@Override

public void onComplete(@NonNull Task<AuthResult> task) {

if (task.isSuccessful()) {

loginProgress.setVisibility(View.INVISIBLE);

btnLogin.setVisibility(View.VISIBLE);

updateUI();
}

else {

showMessage(task.getException().getMessage());

btnLogin.setVisibility(View.VISIBLE);

loginProgress.setVisibility(View.INVISIBLE);

}
}

});

If sign-in succeeded, you can use the returned FirebaseUser to proceed.

  • To sign out a user, call signOut:

public void logout(View view) {
FirebaseAuth.
getInstance().signOut();
}

For Source Code visit my GitHub account

https://github.com/barmangolap15/Sign-In-provider-using-Firebase-in-android-stuido

--

--

Golap Gunjan Barman
The Startup

Hi everyone, myself Golap an Android app developer with UI/UX designer.