From 2058c4bdefb98f93c3b43bcc5ca59a745d581e3a Mon Sep 17 00:00:00 2001 From: HyunWoo Lee Date: Fri, 3 Jan 2025 18:59:43 +0900 Subject: [PATCH] [#1011] Add Google auth laucnher --- .../authenticator/rememberGoogleExecutor.kt | 45 +++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 app/src/main/java/org/sopt/official/feature/auth/authenticator/rememberGoogleExecutor.kt diff --git a/app/src/main/java/org/sopt/official/feature/auth/authenticator/rememberGoogleExecutor.kt b/app/src/main/java/org/sopt/official/feature/auth/authenticator/rememberGoogleExecutor.kt new file mode 100644 index 000000000..76b2ce3c0 --- /dev/null +++ b/app/src/main/java/org/sopt/official/feature/auth/authenticator/rememberGoogleExecutor.kt @@ -0,0 +1,45 @@ +package org.sopt.official.feature.auth.authenticator + +import android.app.Activity +import android.content.Intent +import androidx.activity.compose.ManagedActivityResultLauncher +import androidx.activity.compose.rememberLauncherForActivityResult +import androidx.activity.result.ActivityResult +import androidx.activity.result.contract.ActivityResultContracts +import androidx.compose.runtime.Composable +import androidx.compose.runtime.rememberCoroutineScope +import com.google.android.gms.auth.api.signin.GoogleSignIn +import com.google.android.gms.auth.api.signin.GoogleSignInAccount +import kotlinx.coroutines.launch +import kotlinx.coroutines.tasks.await +import org.sopt.official.common.coroutines.suspendRunCatching +import timber.log.Timber + +@Composable +fun rememberGoogleExecutor( + onSignInSuccess: (GoogleSignInAccount) -> Unit, + onSignInFailed: (Throwable) -> Unit = { Timber.e(it) } +): ManagedActivityResultLauncher { + val scope = rememberCoroutineScope() + + val launcher = rememberLauncherForActivityResult( + contract = ActivityResultContracts.StartActivityForResult() + ) { result -> + if (result.resultCode == Activity.RESULT_OK) { + val data = result.data + scope.launch { + suspendRunCatching { + GoogleSignIn.getSignedInAccountFromIntent(data).await() + }.onSuccess { + onSignInSuccess(it) + }.onFailure { + onSignInFailed(it) + } + } + } else { + Timber.d("Google Sign-In canceled.") + } + } + + return launcher +}