From 266ea84c8558f830fa8ca0a297f9166316ebee28 Mon Sep 17 00:00:00 2001 From: youjin09222 Date: Sun, 12 Jan 2025 08:37:19 +0900 Subject: [PATCH] =?UTF-8?q?#17=20[FEAT]=20=EA=B5=AC=EA=B8=80=20=EB=A1=9C?= =?UTF-8?q?=EA=B7=B8=EC=9D=B8=20=EA=B5=AC=ED=98=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../presentation/auth/login/LoginViewModel.kt | 47 ++++++++++++++++--- 1 file changed, 41 insertions(+), 6 deletions(-) diff --git a/presentation/src/main/java/com/sopt/presentation/auth/login/LoginViewModel.kt b/presentation/src/main/java/com/sopt/presentation/auth/login/LoginViewModel.kt index 0156deb..191df16 100644 --- a/presentation/src/main/java/com/sopt/presentation/auth/login/LoginViewModel.kt +++ b/presentation/src/main/java/com/sopt/presentation/auth/login/LoginViewModel.kt @@ -1,8 +1,11 @@ package com.sopt.presentation.auth.login import android.content.Context +import androidx.activity.result.ActivityResultLauncher +import androidx.activity.result.IntentSenderRequest import com.google.android.gms.auth.api.identity.Identity import com.google.android.gms.auth.api.identity.SignInClient +import com.google.android.gms.auth.api.identity.SignInCredential import com.kakao.sdk.auth.model.OAuthToken import com.kakao.sdk.common.model.ClientError import com.kakao.sdk.common.model.ClientErrorCause @@ -13,10 +16,12 @@ import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow import javax.inject.Inject +import javax.inject.Named @HiltViewModel -class LoginViewModel @Inject constructor() : BaseViewModel() { - +class LoginViewModel @Inject constructor( + @Named("GoogleClientId") private val googleClientId: String +) : BaseViewModel() { private val _authId = MutableStateFlow("") val authId: StateFlow = _authId private lateinit var oneTapClient: SignInClient @@ -60,10 +65,40 @@ class LoginViewModel @Inject constructor() : BaseViewModel() { } } - fun googleLogin() { - // TODO: 구글 로그인 - _authId.value = "google_access_token" - navigateToSignup(_authId.value) + // Google Login + fun googleLogin(launcher: ActivityResultLauncher) { + val signInRequest = com.google.android.gms.auth.api.identity.BeginSignInRequest.builder() + .setGoogleIdTokenRequestOptions( + com.google.android.gms.auth.api.identity.BeginSignInRequest.GoogleIdTokenRequestOptions.builder() + .setSupported(true) + .setServerClientId(googleClientId) + .setFilterByAuthorizedAccounts(false) + .build() + ) + .build() + + oneTapClient.beginSignIn(signInRequest) + .addOnSuccessListener { result -> + launcher.launch(IntentSenderRequest.Builder(result.pendingIntent).build()) + } + .addOnFailureListener { exception -> + emitSideEffect( + LoginSideEffect.ShowToast( + R.string.toast_google_login_failed, + exception.localizedMessage + ) + ) + } + } + + fun handleGoogleLoginResult(credential: SignInCredential) { + if (!credential.googleIdToken.isNullOrEmpty()) { + _authId.value = credential.googleIdToken.toString() + emitSideEffect(LoginSideEffect.ShowToast(R.string.toast_google_login_success)) + navigateToSignup(_authId.value) + } else { + emitSideEffect(LoginSideEffect.ShowToast(R.string.toast_google_login_failed)) + } } private fun navigateToSignup(authId: String) {