Google Sign in with Jetpack Compose
This is part of a series of articles where we will explore something new related to jetpack compose API’s and in this article, we will look into how we can use the rememberLauncherForActivityResult
API by implementing Google sign-in.
rememberLauncherForActivityResult
is basically used to get results from other activity components like launching a camera or document picker or contacts picker based on intents, we will use the same to launch our own custom intent in composable function and get results.
Google Auth Project Creation:
So the first thing that we need is to Configure a Google API Console project that will give us the OAuth 2.0 client ID. Once you have the OAuth 2.0 client ID
paste into your local string XML file.
<string name="gcp_id">553313394578-6uuv2ohetikvrqkb6nekri7en4qbca74.example.com</string>
Add the Google auth SDK app dependencies:
implementation 'com.google.android.gms:play-services-auth:19.2.0'
Let’s write some more code:
Here we will first declare our Google sign-in client object in MainActivity
Now comes the important part where we will declare the rememberLauncherForActivityResult()
in the composable function where signup UI is being added.
Assuming we have some button to launch the google sign-in Signup screen then we can do something like below. Call the newly created startForResult
object to launch the intent inside button onClick lambda function.
And that's it if you run it then you should be able to see the google sign in popup on click of the below button and get the results back in the rememberLauncherForActivityResult
callback.
We have been doing this previously for UIToolkit but since we are adopting Jetpack Compose slowly so many challenges come up on how to do it with Jetpack compose the same task. I hope this small article helps in bridging a small gap in adopting Jetpack Compose. Next time we will learn something new about Jetpack Compose.