Authenticate with Google Sign In

Victor Santander
FW Engineering
Published in
3 min readApr 9, 2019

Tired of writing the same authentication/registration code in all your apps? Tired of including a lot of fields to register on your app?

Keep reading to find out how to fix it with Google Sign In.

This article is the first of some articles that show how to sign in with some SDK (Google, Twitter, Facebook, etc). The Google SDK allows you to take advantage of OAuth and a custom login button to easily integrate it in your app. Here we’re going to look at the steps required to use Google Sign In in your app.

Server configuration

  • Open the Google API Console
  • Create a new project if you don’t have one
  • Click on create credentials button and select OAuth Client Id and Android Application
  • Introduce all the information required like the package name and the SHA-1 certificate fingerprint of the app. To obtain this, in Android Studio we should click on the right side of the screen in the Gradle menu, and click in the signingReport script. Then on the console the SHA-1 of the project will appear.
  • If your app authenticates from a backend server you must get the OAuth 2.0 client id that will be created for your server. To create it, follow the same steps to register the Android application but select Web application.

Android configuration

implementation 'com.google.android.gms:play-services-auth:x.x.x'

After the configuration is done, we are ready to code the solution.

Android project

  • Add the Google Sign in Button in the layout, we can use the button provided by the SDK.
<com.google.android.gms.common.SignInButton
android:id="@+id/btnGoogleLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
  • Create the Google configuration instance using the GoogleSignInOptions builder class.
val gso =  GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN)
.requestEmail()
.build()

The class is created with the DEFAULT_SIGN_IN parameter, and to request the user email we add the requestEmail option also. Here we can also specify any additional scope, for instance the permission to read the gmail account, using the requestScopes option.

In addition if you are using a backend you should include the requestIdToken option adding the server OAuth 2.0 client id which we created previously.

  • Then using this google options instance get the GoogleSignInClient object.
googleSignInClient = GoogleSignIn.getClient(activity, gso)
  • Launch the Google activity to sign in
val signInIntent = googleSignInClient.signInIntent
try {
startActivityForResult(activity, signInIntent, GOOGLE_REQ_CODE, null)

Then when this activity will be launched it will appear the Google screen to add the credentials.

  • Add the onActivityResult method in order to get the result of the Google sign in activity.
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)

if (requestCode == GOOGLE_REQ_CODE && resultCode != Activity.RESULT_CANCELED) {
handleLoginResponse(data)
}
}
fun handleLoginResponse(data: Any?) {
val task = GoogleSignIn.getSignedInAccountFromIntent(data as Intent)
try {
task.getResult(ApiException::class.java)
Log.d("handleLoginResponse ok")
} catch (e: Exception) {
Log.e("handleLoginResponse error: ${e.message}")
}
}

Then from the getResult method we can obtain the user information that we have added in the configuration, in that case the email, the user’s Google Id and the ID token for the user. If you use a backend server you can send the ID token to the backend and validate it on the server side.

  • To logout with Google is very simple, add the following code
googleSignInClient.signOut()
  • And finally in order to know if any user is logged in, if the instance is different than null it means that any user is logged in
fun isUserLoggedIn() : Boolean {
val googleSignInAccount = GoogleSignIn.getLastSignedInAccount(this)
return googleSignInAccount != null
}

Here you can see the example application that we created.

Thanks for reading this article, hope you find this useful!

--

--