How to use Firebase Phone and Google Authentication in Android.

Vikas Kumar
2 min readNov 7, 2020

--

Add Firebase to android studio

  • Create a project on Firebase and then register your app with Firebase by entering the app’s package name in the Android package name field and then click register.
  • Click Download google-services.json to obtain your Firebase Android config file (google-services.json) and paste into the module (app-level) directory of your app.
  • Add the google-services-plugin to your Gradle files.
    In your module(project-level)
buildscript {

repositories {
// Check that you have the following line (if not, add it):
google() // Google's Maven repository

}

dependencies {
// ...

// Add the following line:
classpath 'com.google.gms:google-services:4.3.3' // Google Services plugin

}
}

allprojects {
// ...

repositories {
// Check that you have the following line (if not, add it):
google() // Google's Maven repository

// ...
}
}

In your module(app-level)

apply plugin: 'com.android.application'
// Add the following line:
apply plugin: 'com.google.gms.google-services' // Google Services plugin


android {
// ...
}
  • Add dependencies into gradle.build(app-level)
implementation 'com.google.firebase:firebase-analytics:17.2.2'    implementation 'com.firebaseui:firebase-ui-auth:6.2.0'    implementation 'com.google.firebase:firebase-auth:19.3.0'

Check user is logged in or not

if (FirebaseAuth.getInstance().getCurrentUser()==null){ 
//user is not logged in
}
else {
//User is logged in
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser(); }

Add sign-in and sign out on the button

buttonVariable.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (FirebaseAuth.getInstance().getCurrentUser() == null){
showLogin();
//Toast.makeText(OnBoarding.this, "You are logged in", Toast.LENGTH_SHORT).show();
}
else{
signout();
}
}
});
private void showLogin() {
List<AuthUI.IdpConfig> providers = Arrays.asList(
// new AuthUI.IdpConfig.PhoneBuilder().build(),
new AuthUI.IdpConfig.GoogleBuilder().build()
);

// Create and launch sign-in intent
startActivityForResult(
AuthUI.getInstance()
.createSignInIntentBuilder()
.setAvailableProviders(providers)
.build(),
RC_SIGN_IN);
}
private void signout() {
AuthUI.getInstance()
.signOut(this)
.addOnCompleteListener(new OnCompleteListener<Void>() {
public void onComplete(@NonNull Task<Void> task) {
// User logged out, updare data
Toast.makeText(OnBoarding.this, "Sign out Successfully", Toast.LENGTH_SHORT).show();

}
});
}

At last, overwrite the onActivityResult method

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if (requestCode == RC_SIGN_IN) {
IdpResponse response = IdpResponse.fromResultIntent(data);

if (resultCode == RESULT_OK) {
// Successfully signed in
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
Toast.makeText(this, "Sign In Successful", Toast.LENGTH_SHORT).show();

Intent intent = new Intent(OnBoarding.this, Dashboard.class);
startActivity(intent);
finish();

// ...
} else {
// Sign in failed. If response is null the user canceled the
Toast.makeText(this, "Sign In failed", Toast.LENGTH_SHORT).show();
}
}
}

Congratulations, Firebase Phone and Google Authentication are ready to use.

Resources

  1. https://firebase.google.com/
  2. https://developers.google.com/android/guides/google-services-plugin

Do clap πŸ‘πŸ»πŸ‘πŸ» 50 times and share the article if you like it. Thank you for giving your valuable time :)

--

--

Vikas Kumar

Android Developer | Actions on Google | GCP | Tech-Blogger | Mentor | Open Source Contributor| CA at Coding Blocks