In this tutorial we will learn how to implement google SignIn in android using firebase .The main idea behind using google and facebook login in mobile and web application is for making the signin process easier for users. Using the help of Google SignIn we can get the validated email by google and some extra information about user in a single button click.
Steps
- Configure Firebase application.
- Enable Google SignIn in firebase
- Add SHA – key to firebase
- Download and add google-services.json file to your android application
- Compile the firebase libraries in your build.gradle file
- Adding Google SignIn button to layout.
- Implementing SignIn code and handling result code to class file.
Configure Firebase application
If you already having firebase application configured for your android app ,skip this step. If you don’t have a firebase application for your android application please refer first step in Configure Firebase in for android.
Enable Google SignIn in Firebase application
In this step we need to enable google SignIn for our firebase application. For that in your project panel in firebase click Authentication on left menu , Now you can see 3 tabs on right side ,Select the sign-in-method tab(middle one) and there make google as enabled.
Add SHA – key to firebase
Now if we want google signin working it is important to have SHA key on firebase. For adding SHA -key in your firebase application Go to Settings->General, and scroll down to bottom and click on Add Fingerprint button. There you can add your SHA-key.
Download and add google-services.json file to your android application
After adding SHA-key download the google-services.json file and add to your android project.Paste it in app folder under project.
Now we are ready with firebase application and ready to implement code in android application
Compile the firebase libraries in your build.gradle file
Next step is to have firebase library to your project. Note that it is important to have the latest version of the library.
compile 'com.google.firebase:firebase-auth:11.8.0' compile 'com.google.android.gms:play-services-auth:11.8.0'
Adding Google SignIn button to layout
First stage of design is to have a Google Signin button .You do not need to design a button for this, All you need is to add the following widget to your layout file in activity. This will display a Google SignIn button in your Activity.
<com.google.android.gms.common.SignInButton android:id="@+id/sign_in_button" android:layout_width="wrap_content" android:layout_height="wrap_content" />
Implementing SignIn code and handling result code to class file
Next simply use the following code in your Activity’s class file
import android.content.Intent; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Toast; import com.google.android.gms.auth.api.Auth; import com.google.android.gms.auth.api.signin.GoogleSignInAccount; import com.google.android.gms.auth.api.signin.GoogleSignInOptions; import com.google.android.gms.auth.api.signin.GoogleSignInResult; import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.SignInButton; import com.google.android.gms.common.api.GoogleApiClient; public class GoogleSignin extends AppCompatActivity implements GoogleApiClient.OnConnectionFailedListener{ private GoogleApiClient mGoogleApiClient; private int RC_SIGN_IN = 1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.your_layout_file); GoogleSignInOptions gso = new GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestIdToken(getString(R.string.default_web_client_id)) .requestEmail() .build(); mGoogleApiClient = new GoogleApiClient.Builder(this) .enableAutoManage(this , this ) .addApi(Auth.GOOGLE_SIGN_IN_API, gso) .build(); SignInButton signInButton = (SignInButton) findViewById(R.id.sign_in_button); signInButton.setSize(SignInButton.SIZE_STANDARD); signInButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent signInIntent = Auth.GoogleSignInApi.getSignInIntent(mGoogleApiClient); startActivityForResult(signInIntent, RC_SIGN_IN); } }); } @Override public void onConnectionFailed(@NonNull ConnectionResult connectionResult) { } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == RC_SIGN_IN) { GoogleSignInResult result = Auth.GoogleSignInApi.getSignInResultFromIntent(data); if (result.isSuccess()) { // Signed in successfully GoogleSignInAccount acct = result.getSignInAccount(); String id = acct.getId(); String name = acct.getDisplayName(); String email = acct.getEmail(); Toast.makeText(getApplicationContext(),email,Toast.LENGTH_SHORT).show(); } else { Toast.makeText(getApplicationContext(),"Failed to authenticate user",Toast.LENGTH_SHORT).show(); } } } }
Note: In the onActivityResult method you will get a validated email id and some other information about the user. Using that email id you can authenticate the user and allow him to use your application.