From 950cd03c585358eb4c6ff28a6fb851a63c71e2a1 Mon Sep 17 00:00:00 2001 From: Jan Date: Sat, 23 Dec 2023 23:09:16 +0100 Subject: [PATCH 1/4] Fix CM automatically triggering --- .../compose/auth/composable/GoogleAuth.kt | 71 +++++++++++-------- 1 file changed, 41 insertions(+), 30 deletions(-) diff --git a/plugins/ComposeAuth/src/androidMain/kotlin/io/github/jan/supabase/compose/auth/composable/GoogleAuth.kt b/plugins/ComposeAuth/src/androidMain/kotlin/io/github/jan/supabase/compose/auth/composable/GoogleAuth.kt index be64d0bb..9b7d9b72 100644 --- a/plugins/ComposeAuth/src/androidMain/kotlin/io/github/jan/supabase/compose/auth/composable/GoogleAuth.kt +++ b/plugins/ComposeAuth/src/androidMain/kotlin/io/github/jan/supabase/compose/auth/composable/GoogleAuth.kt @@ -20,6 +20,7 @@ import com.google.android.gms.common.api.ApiException import com.google.android.gms.common.api.CommonStatusCodes import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential import com.google.android.libraries.identity.googleid.GoogleIdTokenParsingException +import io.github.jan.supabase.annotations.SupabaseInternal import io.github.jan.supabase.compose.auth.ComposeAuth import io.github.jan.supabase.compose.auth.GoogleLoginConfig import io.github.jan.supabase.compose.auth.defaultSignOutBehavior @@ -56,46 +57,55 @@ internal fun ComposeAuth.signInWithCM(onResult: (NativeSignInResult) -> Unit, fa val context = LocalContext.current LaunchedEffect(key1 = state.started){ - val activity = context.getActivity() + if (state.started) { + val activity = context.getActivity() - if (activity == null || config.loginConfig["google"] == null) { - fallback.invoke() - state.reset() - return@LaunchedEffect - } + if (activity == null || config.loginConfig["google"] == null) { + fallback.invoke() + state.reset() + return@LaunchedEffect + } - try { - val request = GetCredentialRequest.Builder().addCredentialOption(getGoogleIDOptions(config.loginConfig["google"] as? GoogleLoginConfig)).build() - val result = CredentialManager.create(context).getCredential(activity, request) + try { + val request = GetCredentialRequest.Builder() + .addCredentialOption(getGoogleIDOptions(config.loginConfig["google"] as? GoogleLoginConfig)) + .build() + val result = CredentialManager.create(context).getCredential(activity, request) - when (result.credential) { - is CustomCredential -> { - if (result.credential.type == GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL) { - try { - val googleIdTokenCredential = GoogleIdTokenCredential.createFrom(result.credential.data) - signInWithGoogle(googleIdTokenCredential.idToken) - onResult.invoke(NativeSignInResult.Success) - } catch (e: GoogleIdTokenParsingException) { - onResult.invoke( - NativeSignInResult.Error( - e.localizedMessage ?: "error: google id parsing exception" + when (result.credential) { + is CustomCredential -> { + if (result.credential.type == GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL) { + try { + val googleIdTokenCredential = + GoogleIdTokenCredential.createFrom(result.credential.data) + signInWithGoogle(googleIdTokenCredential.idToken) + onResult.invoke(NativeSignInResult.Success) + } catch (e: GoogleIdTokenParsingException) { + onResult.invoke( + NativeSignInResult.Error( + e.localizedMessage ?: "error: google id parsing exception" + ) ) - ) + } + } else { + onResult.invoke(NativeSignInResult.Error("error: unexpected type of credential")) } - } else { - onResult.invoke(NativeSignInResult.Error("error: unexpected type of credential")) } + + else -> onResult.invoke(NativeSignInResult.Error("error: unsupported credentials")) } - else -> onResult.invoke(NativeSignInResult.Error("error: unsupported credentials")) - } - } catch (e: GetCredentialException) { - when(e){ - is GetCredentialCancellationException -> onResult.invoke(NativeSignInResult.ClosedByUser) - else -> onResult.invoke(NativeSignInResult.Error(e.localizedMessage ?: "error: getCredentialException")) + } catch (e: GetCredentialException) { + when (e) { + is GetCredentialCancellationException -> onResult.invoke(NativeSignInResult.ClosedByUser) + else -> onResult.invoke( + NativeSignInResult.Error( + e.localizedMessage ?: "error: getCredentialException" + ) + ) + } } } - } return state @@ -166,6 +176,7 @@ internal fun ComposeAuth.oneTapSignIn(onResult: (NativeSignInResult) -> Unit, fa /** * Composable for Google SignOut with native behavior */ +@OptIn(SupabaseInternal::class) @Composable actual fun ComposeAuth.rememberSignOutWithGoogle(signOutScope: SignOutScope): NativeSignInState { val context = LocalContext.current From 7b580c61b0401ccf431383207d79603b496b592d Mon Sep 17 00:00:00 2001 From: Jan Date: Sat, 23 Dec 2023 23:10:56 +0100 Subject: [PATCH 2/4] Fix missing state reset --- gradle.properties | 2 +- .../jan/supabase/compose/auth/composable/GoogleAuth.kt | 8 +++++--- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/gradle.properties b/gradle.properties index b69e5dd0..77edb079 100644 --- a/gradle.properties +++ b/gradle.properties @@ -10,4 +10,4 @@ kotlin.experimental.tryK2=false org.jetbrains.compose.experimental.uikit.enabled=true org.jetbrains.compose.experimental.jscanvas.enabled=true -supabase-version = 2.0.1 +supabase-version = 2.0.2-dev diff --git a/plugins/ComposeAuth/src/androidMain/kotlin/io/github/jan/supabase/compose/auth/composable/GoogleAuth.kt b/plugins/ComposeAuth/src/androidMain/kotlin/io/github/jan/supabase/compose/auth/composable/GoogleAuth.kt index 9b7d9b72..411e4c7b 100644 --- a/plugins/ComposeAuth/src/androidMain/kotlin/io/github/jan/supabase/compose/auth/composable/GoogleAuth.kt +++ b/plugins/ComposeAuth/src/androidMain/kotlin/io/github/jan/supabase/compose/auth/composable/GoogleAuth.kt @@ -91,10 +91,10 @@ internal fun ComposeAuth.signInWithCM(onResult: (NativeSignInResult) -> Unit, fa onResult.invoke(NativeSignInResult.Error("error: unexpected type of credential")) } } - - else -> onResult.invoke(NativeSignInResult.Error("error: unsupported credentials")) + else -> { + onResult.invoke(NativeSignInResult.Error("error: unsupported credentials")) + } } - } catch (e: GetCredentialException) { when (e) { is GetCredentialCancellationException -> onResult.invoke(NativeSignInResult.ClosedByUser) @@ -104,6 +104,8 @@ internal fun ComposeAuth.signInWithCM(onResult: (NativeSignInResult) -> Unit, fa ) ) } + } finally { + state.reset() } } } From dadf1b717abef2ddeffc8f6de589c33ff5cab0c8 Mon Sep 17 00:00:00 2001 From: Jan Date: Sat, 23 Dec 2023 23:15:28 +0100 Subject: [PATCH 3/4] fix formatting --- .../github/jan/supabase/compose/auth/composable/GoogleAuth.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/plugins/ComposeAuth/src/androidMain/kotlin/io/github/jan/supabase/compose/auth/composable/GoogleAuth.kt b/plugins/ComposeAuth/src/androidMain/kotlin/io/github/jan/supabase/compose/auth/composable/GoogleAuth.kt index 411e4c7b..254ea798 100644 --- a/plugins/ComposeAuth/src/androidMain/kotlin/io/github/jan/supabase/compose/auth/composable/GoogleAuth.kt +++ b/plugins/ComposeAuth/src/androidMain/kotlin/io/github/jan/supabase/compose/auth/composable/GoogleAuth.kt @@ -90,8 +90,7 @@ internal fun ComposeAuth.signInWithCM(onResult: (NativeSignInResult) -> Unit, fa } else { onResult.invoke(NativeSignInResult.Error("error: unexpected type of credential")) } - } - else -> { + } else -> { onResult.invoke(NativeSignInResult.Error("error: unsupported credentials")) } } From b02a59ce2aa64c590ca4aad7315ae1c25df127d1 Mon Sep 17 00:00:00 2001 From: Jan Date: Sun, 24 Dec 2023 12:08:05 +0100 Subject: [PATCH 4/4] update version --- gradle.properties | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/gradle.properties b/gradle.properties index 77edb079..45e3f7a2 100644 --- a/gradle.properties +++ b/gradle.properties @@ -10,4 +10,4 @@ kotlin.experimental.tryK2=false org.jetbrains.compose.experimental.uikit.enabled=true org.jetbrains.compose.experimental.jscanvas.enabled=true -supabase-version = 2.0.2-dev +supabase-version = 2.0.2