Supabase supports different OAuth providers including Google. On Android there are multiple libraries to use Native Google Auth and figuring out how to use them with Supabase can be complicated! Luckily, there is a supabase-kt plugin which handles everything for you. In this guide you will learn how integrate Compose Auth into your Jetpack Compose Android App.
Gradle KTS
dependencies {
val supabaseVersion = "VERSION"
implementation("io.github.jan-tennert.supabase:compose-auth:$supabaseVersion")
implementation("io.github.jan-tennert.supabase:gotrue-kt:$supabaseVersion")
}
Gradle
dependencies {
def supabase_version = 'VERSION'
implementation 'io.github.jan-tennert.supabase:compose-auth:${supabase_version}'
implementation 'io.github.jan-tennert.supabase:gotrue-kt:${supabase_version}'
}
You can find the newest version of Compose Auth under supabase-kt releases
Compose Auth can also be used on other Compose Multiplatform supported platforms such as JVM, iOS and JS, however they rely on OAuth from GoTrue. This means you can use this library in a common module without using expect/actual declarations.
-
Create a new project in the Google Cloud Console.
-
Setup the OAuth consent screen for your project.
-
Go to credentials and create a new OAuth Client ID for a Web Application and put in your Supabase callback url, which looks like this:
https://SUPABASE_ID.supabase.co/auth/v1/callback
-
Copy the OAuth Client ID and the Client Secret in the Supabase Google Provider settings
-
Create another OAuth Client ID for an Android App with your the package name you specified in your build file/Android manifest and your SHA-1 certificate
You can get the debug SHA-1 certificate by executing
gradlew signingReport
in your app project directory. Copy the string afterSHA1:
.Alternatively, you can use the keytool to get the SHA-1 certificate for a keystore:
keytool -keystore path-to-debug-or-production-keystore -list -v
-
Copy the OAuth Client ID you just created for an Android App and put it in the Authorized Client IDs in your Supabase Dashboard
After you did all previous steps, you can now easily access all your secrets in your app:
val supabaseClient = createSupabaseClient(
supabaseUrl = "SUPABASE_URL",
supabaseKey = "SUPABASE_KEY"
) {
install(GoTrue)
install(ComposeAuth) {
nativeGoogleLogin("WEB_GOOGLE_CLIENT_ID") //Use the Web Client ID, not the Android one!
}
}
in Compose Auth, you create a Auth State which can be later called by Buttons to start the actual Auth flow:
val authState = supabaseClient.composeAuth.rememberLoginWithGoogle()
You can also add a callback for results such as errors:
val authState = supabaseClient.composeAuth.rememberLoginWithGoogle(
onResult = {
when(it) {
NativeSignInResult.ClosedByUser -> errorMessage = "Closed by user"
is NativeSignInResult.Error -> errorMessage = it.message
is NativeSignInResult.NetworkError -> errorMessage = it.message
NativeSignInResult.Success -> println("Success!")
}
}
)
(Optional) When you are programming a Compose Multiplatform app, you can also call this function in the common code and other platforms use GoTrue#loginWith(Google)
as fallback. If you wish to change that logic, you can do so by supplying a custom fallback:
val authState = supabaseClient.composeAuth.rememberLoginWithGoogle(
onResult = {
when(it) {
NativeSignInResult.ClosedByUser -> errorMessage = "Closed by user"
is NativeSignInResult.Error -> errorMessage = it.message
is NativeSignInResult.NetworkError -> errorMessage = it.message
NativeSignInResult.Success -> println("Success!")
}
},
fallback = {
//some custom OAuth flow
}
)
After a user e.g. pressed a Button, you can start the login flow by calling:
authState.startFlow()
That's it! You may also checkout Compose Auth UI which provides multiple composables such as easy to use Provider Buttons, Password Fields, etc.
Hi @jan-tennert ! Should this implementation be enough for iOS too and show the native Google Sign In dialog?
Following this example it works on both platforms but differently, it shows the native Google sign in dialog, but on iOS, it opens the browser application (not a web view inside the app, but a separate browser app) and shows there the "Choose your account" web version.