Skip to content

Commit

Permalink
[#1011] Add Google auth laucnher
Browse files Browse the repository at this point in the history
  • Loading branch information
l2hyunwoo committed Jan 3, 2025
1 parent 2990b06 commit 5dd59a1
Showing 1 changed file with 45 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -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<Intent, ActivityResult> {
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
}

0 comments on commit 5dd59a1

Please sign in to comment.