Sign up with Google and Facebook in your android app

shivam kewat
4 min readJun 24, 2021

--

Welcome folks! Do your app needs user sign up and sign in with solid authentication support, Well it's a no better way to do that with the current most used libraries by Google and Facebook, Not only they are easier to implement but also provides solid support during integration with easy to use dashboard

We will use firebase also to ease our implementation of both libraries,

Let's Get Started ~~

Implement below dependencies

Now connect your app with firebase, Go to android studio -> Tools -> Firebase-> Connect

Then proceed with necessary, don’t forget to include the google-services.json file in the app directory

Make sure to enable authentication methods in the firebase console, both Google and Facebook

Let's first implement google login with the below code

In the above method, We created our GoogleSignInOptions object by using its Builder method and providing necessary details, The main parameter is the second one where we requesting id token with string, It is automatically created when you include google-services.json in your app directory

Let's use the above intent in our ViewModel class

The above method can be called on our MainActivity or Fragment

After calling the method, we get our result in the onActivityResult method, See below ( MainActivity Class )

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_SIGN_IN) {
Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
try {
// Google Sign In was successful, authenticate with Firebase
GoogleSignInAccount account =
task.getResult(ApiException.class);
firebaseAuthWithGoogle(account.getIdToken());
} catch (ApiException e) {

// Google Sign In failed, update UI appropriately
// Log.w(TAG, "Google sign in failed", e);
}
}else {

loginViewModel.callbackManager.onActivityResult(requestCode, resultCode, data);

}
}

Now, we are checking for our request code and getting sign in result data in task object by using GoogleSignIn.getSignedInAccountFromIntent(data);
Then we getting account details by task.getResult(ApiException.class);
Then sending the account token to authenticate with firebase in firebaseAuthWithGoogle(account.getIdToken());

See Below

Now you have your user, do whatever you want with the details

Let's integrate Facebook now

First, register your app in the Facebook console by landing on https://developers.facebook.com/, After doing the necessary steps you will land on the dashboard panel

Here click on next on every step until you reach the below step, put your package name and package location of the LoginActivity where you using Facebook sign in

Now next step is to generate a key hash, Land on this website
http://tomeko.net/online_tools/hex_to_base64.php?lang=en

Get you SHA1 key by going to right edge in android studio -> Gradle -> app -> Tasks -> android -> signinReport
After getting SHA1 copy and paste the key in the above website to generate key hash

You will get something like this — 8O9r4w1SsdfKOj2DUaIQKwsQk=
Now paste this in Facebook and save the details

Back to our ViewModel class, Here are our FB sign in function

We are using Facebook login classes and necessary methods for authenticating our user, After getting successful result token, we authenticate with firebase in handleFacebookAccessToken(loginResult.getAccessToken(),activity);

private void handleFacebookAccessToken(AccessToken token, Activity activity) {
Log.d("TAG", "handleFacebookAccessToken:" + token);

FirebaseAuth mAuth = FirebaseAuth.getInstance();
AuthCredential credential = FacebookAuthProvider.getCredential(token.getToken());
mAuth.signInWithCredential(credential)
.addOnCompleteListener(activity, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
//Sign in successful
FirebaseUser user = mAuth.getCurrentUser();
updateUI(user);

} else {
//Sign in fails
updateUI(null);
}
}
});
}

Congatulations! You have implemented both of the popular logging methods in your application

Be ready for more to rock in the android world

--

--

shivam kewat
shivam kewat

Written by shivam kewat

Hello there Devs! I like building apps for the worlds ...

No responses yet