Skip to content

Commit

Permalink
Merge pull request #395 from supabase-community/fix-cm
Browse files Browse the repository at this point in the history
Fix CM automatically triggering
  • Loading branch information
jan-tennert authored Dec 24, 2023
2 parents 3f5400d + b02a59c commit ae5386b
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 32 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -56,46 +57,56 @@ 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"
)
)
}
} finally {
state.reset()
}
}

}

return state
Expand Down Expand Up @@ -166,6 +177,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
Expand Down

0 comments on commit ae5386b

Please sign in to comment.