Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 0 additions & 4 deletions data/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ android {
testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
buildConfigField("String", "APP_VERSION", "\"${libs.versions.versionName.get()}\"")
buildConfigField("String", "O_AUTH_WEB_CLIENT_ID", getApiKey("o_auth_web_client_id"))
}

buildTypes {
Expand Down Expand Up @@ -76,9 +75,6 @@ dependencies {
implementation(platform(libs.firebase.bom))
implementation(libs.firebase.config.ktx)
implementation(libs.firebase.analytics.ktx)
implementation(libs.credentials)
implementation(libs.credentials.auth)
implementation(libs.google.identity)

debugImplementation(libs.bundles.flipper)
releaseImplementation(libs.flipper.noop)
Expand Down
23 changes: 0 additions & 23 deletions data/src/main/java/com/nextroom/nextroom/data/di/FirebaseModule.kt
Original file line number Diff line number Diff line change
@@ -1,13 +1,9 @@
package com.nextroom.nextroom.data.di

import android.content.Context
import androidx.credentials.CredentialManager
import androidx.credentials.GetCredentialRequest
import com.google.android.libraries.identity.googleid.GetGoogleIdOption
import com.google.firebase.analytics.FirebaseAnalytics
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
import com.google.firebase.remoteconfig.remoteConfigSettings
import com.nextroom.nextroom.data.BuildConfig
import com.nextroom.nextroom.data.util.UserEventLoggerImpl
import com.nextroom.nextroom.domain.util.UserEventLogger
import dagger.Module
Expand Down Expand Up @@ -45,23 +41,4 @@ object FirebaseModule {
fun provideUserEventLogger(firebaseAnalytics: FirebaseAnalytics): UserEventLogger {
return UserEventLoggerImpl(firebaseAnalytics)
}

@Singleton
@Provides
fun provideGetCredentialRequest(): GetCredentialRequest {
val googleIdOption = GetGoogleIdOption.Builder()
.setFilterByAuthorizedAccounts(false)
.setServerClientId(BuildConfig.O_AUTH_WEB_CLIENT_ID)
.build()

return GetCredentialRequest.Builder()
.addCredentialOption(googleIdOption)
.build()
}

@Singleton
@Provides
fun provideCredentialManager(@ApplicationContext context: Context): CredentialManager {
return CredentialManager.create(context)
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
package com.nextroom.nextroom.data.di

import android.content.Context
import androidx.credentials.CredentialManager
import androidx.credentials.GetCredentialRequest
import com.google.firebase.remoteconfig.FirebaseRemoteConfig
import com.nextroom.nextroom.data.datasource.AuthDataSource
import com.nextroom.nextroom.data.datasource.BillingDataSource
Expand Down Expand Up @@ -122,19 +120,13 @@ object RepositoryModule {
settingDataSource: SettingDataSource,
tokenDataSource: TokenDataSource,
subscriptionDataSource: SubscriptionDataSource,
getCredentialRequest: GetCredentialRequest,
credentialManager: CredentialManager,
@ApplicationContext context: Context,
apiService: ApiService,
): AdminRepository = AdminRepositoryImpl(
authDataSource = authDataSource,
userDataSource = userDataSource,
settingDataSource = settingDataSource,
tokenDataSource = tokenDataSource,
subscriptionDataSource = subscriptionDataSource,
getCredentialRequest = getCredentialRequest,
credentialManager = credentialManager,
context = context,
apiService = apiService,
)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,5 @@
package com.nextroom.nextroom.data.repository

import android.content.Context
import androidx.credentials.CredentialManager
import androidx.credentials.CustomCredential
import androidx.credentials.GetCredentialRequest
import androidx.credentials.GetCredentialResponse
import com.google.android.libraries.identity.googleid.GoogleIdTokenCredential
import com.google.android.libraries.identity.googleid.GoogleIdTokenParsingException
import com.nextroom.nextroom.data.datasource.AuthDataSource
import com.nextroom.nextroom.data.datasource.SettingDataSource
import com.nextroom.nextroom.data.datasource.SubscriptionDataSource
Expand All @@ -16,7 +9,6 @@ import com.nextroom.nextroom.data.network.ApiService
import com.nextroom.nextroom.data.network.response.AdditionalUserInfoRequestDto
import com.nextroom.nextroom.data.network.response.GoogleLoginRequestDto
import com.nextroom.nextroom.domain.model.AdditionalUserInfoResponse
import com.nextroom.nextroom.domain.model.GoogleAuthResponse
import com.nextroom.nextroom.domain.model.GoogleLoginResponse
import com.nextroom.nextroom.domain.model.LoginInfo
import com.nextroom.nextroom.domain.model.Mypage
Expand All @@ -36,9 +28,6 @@ class AdminRepositoryImpl @Inject constructor(
private val settingDataSource: SettingDataSource,
private val tokenDataSource: TokenDataSource,
private val subscriptionDataSource: SubscriptionDataSource,
private val getCredentialRequest: GetCredentialRequest,
private val credentialManager: CredentialManager,
private val context: Context,
private val apiService: ApiService,
) : AdminRepository {

Expand All @@ -64,39 +53,6 @@ class AdminRepositoryImpl @Inject constructor(
}
}

override suspend fun requestGoogleAuth(): Result<GoogleAuthResponse> {
val result = credentialManager.getCredential(
request = getCredentialRequest,
context = context,
)
return handleSignIn(result)
}

private fun handleSignIn(result: GetCredentialResponse): Result.Success<GoogleAuthResponse> {
return when (val credential = result.credential) {
is CustomCredential -> {
if (credential.type == GoogleIdTokenCredential.TYPE_GOOGLE_ID_TOKEN_CREDENTIAL) {
try {
val googleIdTokenCredential = GoogleIdTokenCredential.createFrom(credential.data)
Result.Success(
GoogleAuthResponse(
idToken = googleIdTokenCredential.idToken,
email = googleIdTokenCredential.id,
name = googleIdTokenCredential.displayName,
)
)
} catch (e: GoogleIdTokenParsingException) {
throw Exception("handleSignIn received an invalid google id token response", e)
}
} else {
throw Exception("unexpected type of credential")
}
}

else -> throw Exception("unexpected type of credential")
}
}

override suspend fun logout() {
authDataSource.logout()
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.nextroom.nextroom.domain.repository

import com.nextroom.nextroom.domain.model.AdditionalUserInfoResponse
import com.nextroom.nextroom.domain.model.GoogleAuthResponse
import com.nextroom.nextroom.domain.model.GoogleLoginResponse
import com.nextroom.nextroom.domain.model.LoginInfo
import com.nextroom.nextroom.domain.model.Mypage
Expand All @@ -22,7 +21,6 @@ interface AdminRepository {
* @return shopName
* */
suspend fun login(adminCode: String, password: String, emailSaveChecked: Boolean): Result<LoginInfo>
suspend fun requestGoogleAuth(): Result<GoogleAuthResponse>
suspend fun logout()
suspend fun resign(): Result<Unit>
suspend fun verifyAdminCode(code: String): Boolean
Expand Down
7 changes: 7 additions & 0 deletions presentation/build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import com.android.build.gradle.internal.cxx.configure.gradleLocalProperties

@Suppress("DSL_SCOPE_VIOLATION")
plugins {
alias(libs.plugins.android.library)
Expand All @@ -17,6 +19,7 @@ android {

testInstrumentationRunner = "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles("consumer-rules.pro")
buildConfigField("String", "O_AUTH_WEB_CLIENT_ID", getApiKey("o_auth_web_client_id"))
}

buildTypes {
Expand Down Expand Up @@ -91,3 +94,7 @@ dependencies {
testImplementation(libs.junit)
androidTestImplementation(libs.bundles.android.test)
}

fun getApiKey(propertyKey: String): String {
return gradleLocalProperties(rootDir).getProperty(propertyKey)
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.ui.platform.ComposeView
import androidx.compose.ui.platform.ViewCompositionStrategy
import androidx.credentials.exceptions.GetCredentialCancellationException
import androidx.fragment.app.viewModels
import androidx.lifecycle.lifecycleScope
import androidx.navigation.fragment.findNavController
import com.nextroom.nextroom.presentation.NavGraphDirections
import com.nextroom.nextroom.presentation.R
Expand All @@ -21,15 +23,22 @@ import com.nextroom.nextroom.presentation.extension.snackbar
import com.nextroom.nextroom.presentation.extension.toast
import com.nextroom.nextroom.presentation.ui.login.compose.EmailLoginScreen
import com.nextroom.nextroom.presentation.ui.onboarding.LoginFragment.Companion.SIGNUP_REQUEST_KEY
import com.nextroom.nextroom.presentation.util.GoogleAuthClient
import com.nextroom.nextroom.presentation.util.Logger
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import javax.inject.Inject
import kotlin.coroutines.cancellation.CancellationException

@AndroidEntryPoint
class EmailLoginFragment : ComposeBaseViewModelFragment<EmailLoginViewModel>() {

override val screenName = "email_login"
override val viewModel: EmailLoginViewModel by viewModels()

@Inject
lateinit var googleAuthClient: GoogleAuthClient

override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
Expand All @@ -45,7 +54,7 @@ class EmailLoginFragment : ComposeBaseViewModelFragment<EmailLoginViewModel>() {
onPasswordChange = viewModel::inputPassword,
onEmailSaveCheckedChange = viewModel::onEmailSaveChecked,
onLoginClick = viewModel::complete,
onGoogleLoginClick = viewModel::requestGoogleAuth,
onGoogleLoginClick = ::requestGoogleAuth,
onBackClick = { findNavController().navigateUp() },
onCustomerServiceClick = ::openCustomerService,
onSignupClick = ::openSignupWebView,
Expand All @@ -67,14 +76,41 @@ class EmailLoginFragment : ComposeBaseViewModelFragment<EmailLoginViewModel>() {
}
}

/**
* Credential Manager는 계정 선택 UI를 띄우기 위해 Activity를 필요로 한다.
* Activity 참조가 화면 밖으로 새어나가지 않도록 viewLifecycleOwner 스코프 안에서만 다룬다.
*/
private fun requestGoogleAuth() {
viewLifecycleOwner.lifecycleScope.launch {
// 계정 선택 UI가 뜨기까지 시간이 걸리므로 요청 시작 시점부터 로딩을 노출한다.
viewModel.setGoogleAuthLoading(true)
val idToken = try {
googleAuthClient.requestGoogleIdToken(requireActivity())
} catch (e: Exception) {
// 성공하면 loginWithGoogle이 로딩을 이어받으므로, 실패한 경우에만 해제한다.
viewModel.setGoogleAuthLoading(false)
when (e) {
// 사용자가 계정 선택을 취소한 경우이므로 오류로 처리하지 않는다.
is GetCredentialCancellationException -> Unit
is CancellationException -> throw e
else -> {
Logger.e(e)
toast(R.string.error_something)
}
}
return@launch
}
viewModel.loginWithGoogle(idToken)
}
}

private fun handleEvent(event: EmailLoginViewModel.UiEvent) {
when (event) {
is EmailLoginViewModel.UiEvent.ShowMessage ->
snackbar(event.message.toString(requireContext()))

is EmailLoginViewModel.UiEvent.EmailLoginFailed -> snackbar(event.message)

EmailLoginViewModel.UiEvent.GoogleAuthFailed,
EmailLoginViewModel.UiEvent.GoogleLoginFailed -> toast(R.string.error_something)

is EmailLoginViewModel.UiEvent.NeedAdditionalUserInfo -> moveToSignup()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package com.nextroom.nextroom.presentation.ui.login

import androidx.annotation.StringRes
import androidx.credentials.exceptions.GetCredentialCancellationException
import androidx.lifecycle.viewModelScope
import com.nextroom.nextroom.domain.model.Result
import com.nextroom.nextroom.domain.model.onFailure
Expand Down Expand Up @@ -94,23 +93,19 @@ class EmailLoginViewModel @Inject constructor(
}
}

fun requestGoogleAuth() {
baseViewModelScope.launch {
_uiState.update { it.copy(loading = true) }
try {
adminRepository.requestGoogleAuth().getOrThrow
.also { postGoogleLogin(it.idToken) }
} catch (e: GetCredentialCancellationException) {
// do nothing
} catch (e: Exception) {
_uiEvent.emit(UiEvent.GoogleAuthFailed)
}
_uiState.update { it.copy(loading = false) }
}
/**
* 계정 선택 UI가 뜨기 전까지도 로딩을 노출하기 위해, 자격증명을 요청하는 Fragment가 로딩 시작/해제를 알린다.
*/
fun setGoogleAuthLoading(loading: Boolean) {
_uiState.update { it.copy(loading = loading) }
}

private fun postGoogleLogin(idToken: String) {
/**
* 구글 계정 선택은 Activity가 필요하므로 Fragment가 담당하고, 여기서는 발급받은 id token으로 로그인만 처리한다.
*/
fun loginWithGoogle(idToken: String) {
baseViewModelScope.launch {
_uiState.update { it.copy(loading = true) }
try {
adminRepository.postGoogleLogin(idToken).getOrThrow.let {
if (!it.isComplete) {
Expand All @@ -120,6 +115,7 @@ class EmailLoginViewModel @Inject constructor(
} catch (e: Exception) {
_uiEvent.emit(UiEvent.GoogleLoginFailed)
}
_uiState.update { it.copy(loading = false) }
}
}

Expand All @@ -138,7 +134,6 @@ class EmailLoginViewModel @Inject constructor(
sealed interface UiEvent {
data class EmailLoginFailed(val message: String) : UiEvent
data class ShowMessage(val message: UiText) : UiEvent
data object GoogleAuthFailed : UiEvent
data object GoogleLoginFailed : UiEvent
data class NeedAdditionalUserInfo(val shopName: String?) : UiEvent
}
Expand Down
Loading
Loading