Firebase Authentication Task
Firebase Authentication Task
In this lab tutorial, we will implement login and registration system in Android applications
using Firebase Authentication.
Firebase is a popular choice for Android developers because it offers a suite of cloud-based
services that make app development easier, faster, and more scalable. Key features include:
Firebase helps build secure, scalable apps quickly with less backend work. We will use Firebase
Authentication in this task.
Step-2: Now you need to go to Android Studio, Create an empty project. Go to “Tools”. Choose
Firebase.
Step-3:
Click “Connect your app to Firebase”, a pop up window will be opened in your browser.
Choose the app in your firebase control that you have created in Step-1.
Click “Add the Firebase Authentication SDK to your app”.
Once both of the process are marked done, you can check in the Gradle files to see if the
dependencies are added properly. If not, you have to do it manually.
Step-4: Now you have ‘MainActivity’ and ‘layout_main.xml’ in your android project. Create
two more activities for Sign Up and Profile Page. You can design your login, signup and profile
page as per your requirements. For now, you can use the xml files provided and make necessary
changes in your app. (activity_main.xml, activity_sign_up.xml, activity_logout.xml)
Step-5: Add the action bar to your app. Go to ‘AndroidManifest.xml’ and replace the theme
portion to this line:
android:theme="@style/Theme.AppCompat.Light.DarkActionBar"
go to each activity classes and inside ‘onCreate’ function add the following line:
Now initialize objects for your EditText, TextView and Button widgets that you have created in
your layout and connect their IDs. In addition to these objects you also need to initialize the
ProgressBar and FirebaseAuth objects.
ProgressBar signUpProgressBar;
FirebaseAuth mAuth;
mAuth = FirebaseAuth.getInstance();
signUpProgressBar = findViewById(R.id.sprogressBar);
Implement Button click listener method for your “Sign Up” button and TextView click listener
method for your ‘Sign In’ redirect text. Inside the Sign in Redirect click method, you can
change the intent to your sign in activity.
signInRedirect.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),
MainActivity.class);
startActivity(intent);
finish();
}
});
signUpProgressBar.setVisibility(View.VISIBLE);
Add various constraints to check the user input is according to your requirements.
String email, password;
email = String.valueOf(signUpEmailEditText.getText());
password = String.valueOf(signUpPasswordEditText.getText());
if(TextUtils.isEmpty(email)){
Toast.makeText(SignUpActivity.this, "Enter Email",
Toast.LENGTH_SHORT).show();
return;
}
if(TextUtils.isEmpty(password)){
Toast.makeText(SignUpActivity.this, "Enter Password",
Toast.LENGTH_SHORT).show();
return;
}
Then you need to do the Firebase authentication task for Sign up. go to
https://firebase.google.com/docs/auth/android/password-auth and copy the following code to
paste inside the onClick method.
} else {
// If sign in fails, display a message to the user.
}
}
});
Add the necessary packages by clicking Alt+Enter if any portion of the code segment is Red.
Now if the “sign up” is successful, you can add any Toast Message and also change the intent
to your sign in activity class. Also, you can show Toast Message when the “sign up” attempt
fails.
@Override
public void onStart() {
super.onStart();
FirebaseUser currentUser = mAuth.getCurrentUser();
if(currentUser != null){
Intent intent = new Intent(getApplicationContext(), logout.class);
startActivity(intent);
finish();
}
}
This code is part of an Android Activity that checks if a user is already logged in when the
activity starts.
onStart(): This method runs every time the activity becomes visible to the user.
mAuth.getCurrentUser(): Checks if a user is currently logged in (returns null if no user is
logged in).
If currentUser is not null: It means the user is logged in. The code then:
o Creates an Intent to navigate to the logout activity.
o Starts the logout activity.
o Calls finish() to close the current activity.
Same as Step-6. Make necessary changes for sign in instead of sign up.
Same as Step-7. Make necessary changes for sign in instead of sign up.
Then you need to do the Firebase authentication task for log in. go to
https://firebase.google.com/docs/auth/android/password-auth and copy the following code to
paste inside the onClick method.
Add the necessary packages by clicking Alt+Enter if any portion of the code segment is Red.
Important: Add the following function before ‘onCreate’ function:
@Override
public void onStart() {
super.onStart();
FirebaseUser currentUser = mAuth.getCurrentUser();
if(currentUser != null){
Intent intent = new Intent(getApplicationContext(), logout.class);
startActivity(intent);
finish();
}
}
Initialize objects and connect IDs for your Button (logout), TextView (to show email). Also
initialize FirebaseAuth and FirebaseUser object too.
FirebaseAuth mAuth;
FirebaseUser user;
mAuth = FirebaseAuth.getInstance();
user = mAuth.getCurrentUser();
// user stores the information associated with the user currently logged in.
So you can check if the user is NULL or not. If null, you can switch the activity to sign in page.
Otherwise you can display the email of the user.
if (user == null){
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
}
else {
showEmail.setText(user.getEmail());
}
Implement ‘Log Out’ button click listener and inside onClick method: sign out the mAuth
instance and change the intent to sign in page.
mAuth.signOut();
Intent intent = new Intent(getApplicationContext(), MainActivity.class);
startActivity(intent);
finish();
The login and registration system in Android applications using Firebase Authentication is
now complete. Run the app and check its functionality. Thank you. 😊