Google Sign-In guide¶
This guide will walk you through on how to display a screen on your watch app so that users can select their Google account to sign-in to your app.
Requirements¶
Follow the setup instructions for integrating Google Sign-in into an Android app from this link.
Getting started¶
-
Add dependencies
Add the following dependencies to your project’s build.gradle:
dependencies { implementation "com.google.android.horologist:horologist-auth-composables:<version>" implementation "com.google.android.horologist:horologist-auth-ui:<version>" implementation "com.google.android.horologist:horologist-compose-material:<version>" }
-
Create an instance of
GoogleSignInClient
Create an instance of GoogleSignInClient, according to your requirements, for example:
val googleSignInClient = GoogleSignIn.getClient( applicationContext, GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) .requestEmail() .build() )
Display the screen¶
-
Create a ViewModel
Create your implementation of GoogleSignInViewModel, passing the
GoogleSignInClient
created:class MyGoogleSignInViewModel( googleSignInClient: GoogleSignInClient, ) : GoogleSignInViewModel(googleSignInClient)
-
Display the screen
Display the GoogleSignInScreen passing an instance of the
GoogleSignInViewModel
created:GoogleSignInScreen( onAuthCancelled = { /* code to navigate to another screen on this event */ }, onAuthSucceed = { /* code to navigate to another screen on this event */ }, viewModel = hiltViewModel<MyGoogleSignInViewModel>() )
This sample uses Hilt to retrieve an instance of the ViewModel, but you should use what suits your project best, see this link for more info.
Retrieve the signed in account¶
In order to have access an instance of the GoogleSignInAccount selected by the user, follow the steps:
-
Implement
GoogleSignInEventListener
class GoogleSignInEventListenerImpl : GoogleSignInEventListener { override suspend fun onSignedIn(account: GoogleSignInAccount) { // your implementation using the account parameter } }
-
Pass the listener to the ViewModel
Pass an instance of
GoogleSignInEventListener
toGoogleSignInViewModel
:class MyGoogleSignInViewModel( googleSignInClient: GoogleSignInClient, googleSignInEventListener: GoogleSignInEventListener, ) : GoogleSignInViewModel(googleSignInClient, googleSignInEventListener)