From af0ba8b7cc5b9cebce59b1ed92bfc24866eb2e63 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=EA=B9=80=EB=AA=85=EC=84=9D?= Date: Sat, 4 Mar 2023 02:50:40 +0900 Subject: [PATCH] =?UTF-8?q?Issues=20#287=20feat:=20=EB=8B=A4=EB=A5=B8=20?= =?UTF-8?q?=EB=A1=9C=EA=B7=B8=EC=9D=B8=20=EC=A0=9C=EA=B3=B5=20=EC=97=85?= =?UTF-8?q?=EC=B2=B4=EB=A5=BC=20=EC=B6=94=EA=B0=80=ED=95=98=EA=B8=B0?= =?UTF-8?q?=EC=9C=84=ED=95=B4=20=EA=B5=AC=EC=A1=B0=20=EA=B0=9C=EC=84=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- auth-google/build.gradle.kts | 2 +- .../lighthouse/auth/google/di/DataModule.kt | 12 +++- .../auth/google/model/GoogleAuthEvent.kt | 8 --- .../auth/google/repository/GoogleClient.kt | 6 +- ...ClientImpl.kt => GoogleOAuthRepository.kt} | 40 +++++------ .../java/com/lighthouse/auth/di/DataModule.kt | 7 -- .../com/lighthouse/auth/di/OAuthQualifier.kt | 7 ++ .../auth/mapper/AuthEncryptResultMapper.kt | 8 --- .../auth/repository/AuthRepositoryImpl.kt | 50 +++++++++++--- .../auth/repository/OAuthRepository.kt | 10 +++ data/data-preference/build.gradle.kts | 2 + .../UserPreferenceRepositoryImpl.kt | 22 +----- data/data/build.gradle.kts | 2 + .../user/UserPreferenceRepository.kt | 6 +- .../repository/user/UserRepositoryImpl.kt | 67 ++++++------------- .../domain/repository/auth/AuthRepository.kt | 8 ++- .../domain/repository/user/UserRepository.kt | 9 +-- .../edit/GetGifticonForUpdateUseCase.kt | 19 ------ .../gifticon/edit/ModifyGifticonUseCase.kt | 2 +- .../domain/usecase/user/GetUserIdUseCase.kt | 2 +- .../domain/usecase/user/IsLoginUserUseCase.kt | 5 +- .../domain/usecase/user/LoginUseCase.kt | 16 ----- .../domain/usecase/user/SignInUseCase.kt | 15 +++++ .../domain/usecase/user/SignOutUseCase.kt | 9 +-- .../usecase/user/TransferDataUseCase.kt | 7 +- .../domain/usecase/user/WithdrawalUseCase.kt | 5 +- features/ui-common/build.gradle.kts | 3 + .../features/common/ui/AuthViewModel.kt | 64 ++++++++---------- .../features/common/ui/MessageViewModel.kt | 6 ++ .../ui-common/src/main/res/values/strings.xml | 11 +++ features/ui-intro/build.gradle.kts | 3 + .../features/intro/ui/IntroFragment.kt | 34 +++++----- features/ui-setting/build.gradle.kts | 3 + .../features/setting/ui/SettingFragment.kt | 49 +++++--------- libs/ciphertool/.gitignore | 1 + .../beep/model/auth/AuthProvider.kt | 6 ++ .../exception}/AuthenticationException.kt | 2 +- .../auth}/exception/FailedConnectException.kt | 2 +- .../FailedSaveLoginUserException.kt | 2 +- .../exception}/InvalidUserException.kt | 2 +- 40 files changed, 252 insertions(+), 282 deletions(-) delete mode 100644 auth-google/src/main/java/com/lighthouse/auth/google/model/GoogleAuthEvent.kt rename auth-google/src/main/java/com/lighthouse/auth/google/repository/{GoogleClientImpl.kt => GoogleOAuthRepository.kt} (72%) create mode 100644 auth/src/main/java/com/lighthouse/auth/di/OAuthQualifier.kt delete mode 100644 auth/src/main/java/com/lighthouse/auth/mapper/AuthEncryptResultMapper.kt create mode 100644 auth/src/main/java/com/lighthouse/auth/repository/OAuthRepository.kt delete mode 100644 domain/src/main/java/com/lighthouse/domain/usecase/gifticon/edit/GetGifticonForUpdateUseCase.kt delete mode 100644 domain/src/main/java/com/lighthouse/domain/usecase/user/LoginUseCase.kt create mode 100644 domain/src/main/java/com/lighthouse/domain/usecase/user/SignInUseCase.kt rename auth-google/src/main/java/com/lighthouse/auth/google/ui/GoogleAuthViewModel.kt => features/ui-common/src/main/java/com/lighthouse/features/common/ui/AuthViewModel.kt (56%) create mode 100644 libs/ciphertool/.gitignore create mode 100644 model/src/main/java/com/lighthouse/beep/model/auth/AuthProvider.kt rename model/src/main/java/com/lighthouse/beep/model/{exception/auth => auth/exception}/AuthenticationException.kt (68%) rename {auth-google/src/main/java/com/lighthouse/auth/google => model/src/main/java/com/lighthouse/beep/model/auth}/exception/FailedConnectException.kt (70%) rename model/src/main/java/com/lighthouse/beep/model/{exception/auth => auth/exception}/FailedSaveLoginUserException.kt (72%) rename model/src/main/java/com/lighthouse/beep/model/{exception/auth => auth/exception}/InvalidUserException.kt (68%) diff --git a/auth-google/build.gradle.kts b/auth-google/build.gradle.kts index 88ba0d8c5..010d18f43 100644 --- a/auth-google/build.gradle.kts +++ b/auth-google/build.gradle.kts @@ -14,7 +14,7 @@ dependencies { implementation(projects.model) implementation(projects.common) implementation(projects.commonAndroid) - implementation(projects.domain) + implementation(projects.auth) implementation(libs.androidX.fragment.ktx) diff --git a/auth-google/src/main/java/com/lighthouse/auth/google/di/DataModule.kt b/auth-google/src/main/java/com/lighthouse/auth/google/di/DataModule.kt index a4e3f4fca..03ef9af9d 100644 --- a/auth-google/src/main/java/com/lighthouse/auth/google/di/DataModule.kt +++ b/auth-google/src/main/java/com/lighthouse/auth/google/di/DataModule.kt @@ -1,7 +1,9 @@ package com.lighthouse.auth.google.di +import com.lighthouse.auth.di.GoogleOAuth import com.lighthouse.auth.google.repository.GoogleClient -import com.lighthouse.auth.google.repository.GoogleClientImpl +import com.lighthouse.auth.google.repository.GoogleOAuthRepository +import com.lighthouse.auth.repository.OAuthRepository import dagger.Binds import dagger.Module import dagger.hilt.InstallIn @@ -14,6 +16,12 @@ internal abstract class DataModule { @Binds abstract fun bindsGoogleClient( - client: GoogleClientImpl + client: GoogleOAuthRepository ): GoogleClient + + @GoogleOAuth + @Binds + abstract fun bindsGoogleOAuthRepository( + repository: GoogleOAuthRepository + ): OAuthRepository } diff --git a/auth-google/src/main/java/com/lighthouse/auth/google/model/GoogleAuthEvent.kt b/auth-google/src/main/java/com/lighthouse/auth/google/model/GoogleAuthEvent.kt deleted file mode 100644 index 118a25e5a..000000000 --- a/auth-google/src/main/java/com/lighthouse/auth/google/model/GoogleAuthEvent.kt +++ /dev/null @@ -1,8 +0,0 @@ -package com.lighthouse.auth.google.model - -import com.lighthouse.core.android.utils.resource.UIText - -sealed class GoogleAuthEvent { - - data class SnackBar(val text: UIText) : GoogleAuthEvent() -} diff --git a/auth-google/src/main/java/com/lighthouse/auth/google/repository/GoogleClient.kt b/auth-google/src/main/java/com/lighthouse/auth/google/repository/GoogleClient.kt index 3468738f0..0d0e5148a 100644 --- a/auth-google/src/main/java/com/lighthouse/auth/google/repository/GoogleClient.kt +++ b/auth-google/src/main/java/com/lighthouse/auth/google/repository/GoogleClient.kt @@ -2,13 +2,11 @@ package com.lighthouse.auth.google.repository import android.content.Intent import androidx.activity.result.ActivityResult -import com.google.firebase.auth.AuthResult +import com.google.firebase.auth.AuthCredential interface GoogleClient { fun signInIntent(): Intent - suspend fun signIn(result: ActivityResult): Result - - suspend fun signOut(): Result + suspend fun getAuthCredential(result: ActivityResult): Result } diff --git a/auth-google/src/main/java/com/lighthouse/auth/google/repository/GoogleClientImpl.kt b/auth-google/src/main/java/com/lighthouse/auth/google/repository/GoogleOAuthRepository.kt similarity index 72% rename from auth-google/src/main/java/com/lighthouse/auth/google/repository/GoogleClientImpl.kt rename to auth-google/src/main/java/com/lighthouse/auth/google/repository/GoogleOAuthRepository.kt index bd94132e4..0e499264b 100644 --- a/auth-google/src/main/java/com/lighthouse/auth/google/repository/GoogleClientImpl.kt +++ b/auth-google/src/main/java/com/lighthouse/auth/google/repository/GoogleOAuthRepository.kt @@ -7,21 +7,22 @@ import androidx.activity.result.ActivityResult import com.google.android.gms.auth.api.signin.GoogleSignIn import com.google.android.gms.auth.api.signin.GoogleSignInAccount import com.google.android.gms.auth.api.signin.GoogleSignInOptions -import com.google.firebase.auth.AuthResult +import com.google.firebase.auth.AuthCredential import com.google.firebase.auth.GoogleAuthProvider import com.google.firebase.auth.ktx.auth import com.google.firebase.ktx.Firebase import com.lighthouse.auth.google.R -import com.lighthouse.auth.google.exception.FailedConnectException +import com.lighthouse.beep.model.auth.exception.FailedConnectException +import com.lighthouse.auth.repository.OAuthRepository import dagger.hilt.android.qualifiers.ApplicationContext import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.flow.callbackFlow import kotlinx.coroutines.flow.first import javax.inject.Inject -internal class GoogleClientImpl @Inject constructor( +internal class GoogleOAuthRepository @Inject constructor( @ApplicationContext context: Context -) : GoogleClient { +) : OAuthRepository, GoogleClient { private val googleSignInClient by lazy { val gso = GoogleSignInOptions.Builder(GoogleSignInOptions.DEFAULT_SIGN_IN) @@ -45,30 +46,31 @@ internal class GoogleClientImpl @Inject constructor( awaitClose() }.first() - private suspend fun signInWithAccount(account: GoogleSignInAccount): AuthResult { - val credential = GoogleAuthProvider.getCredential(account.idToken, null) - return callbackFlow { - Firebase.auth.signInWithCredential(credential).addOnCompleteListener { - if (it.isSuccessful) { - trySend(it.result) - } - close(it.exception) - } - awaitClose() - }.first() - } - - override suspend fun signIn(result: ActivityResult): Result { + override suspend fun getAuthCredential(result: ActivityResult): Result { return if (result.resultCode == Activity.RESULT_OK) { runCatching { val account = getGoogleAccount(result) - signInWithAccount(account) + GoogleAuthProvider.getCredential(account.idToken, null) } } else { Result.failure(FailedConnectException()) } } + override suspend fun signIn(credential: AuthCredential): Result { + return runCatching { + callbackFlow { + Firebase.auth.signInWithCredential(credential).addOnCompleteListener { + if (it.isSuccessful) { + trySend(Unit) + } + close(it.exception) + } + awaitClose() + }.first() + } + } + override suspend fun signOut(): Result { Firebase.auth.currentUser ?: return Result.success(Unit) return runCatching { diff --git a/auth/src/main/java/com/lighthouse/auth/di/DataModule.kt b/auth/src/main/java/com/lighthouse/auth/di/DataModule.kt index ac451e5aa..6b1313be5 100644 --- a/auth/src/main/java/com/lighthouse/auth/di/DataModule.kt +++ b/auth/src/main/java/com/lighthouse/auth/di/DataModule.kt @@ -1,9 +1,7 @@ package com.lighthouse.auth.di import com.lighthouse.auth.repository.AuthRepositoryImpl -import com.lighthouse.auth.repository.CipherToolImpl import com.lighthouse.domain.repository.auth.AuthRepository -import com.lighthouse.domain.repository.auth.CipherTool import dagger.Binds import dagger.Module import dagger.hilt.InstallIn @@ -18,9 +16,4 @@ internal abstract class DataModule { abstract fun bindsAuthRepository( repository: AuthRepositoryImpl ): AuthRepository - - @Binds - abstract fun bindsCipherTool( - tool: CipherToolImpl - ): CipherTool } diff --git a/auth/src/main/java/com/lighthouse/auth/di/OAuthQualifier.kt b/auth/src/main/java/com/lighthouse/auth/di/OAuthQualifier.kt new file mode 100644 index 000000000..87c7ee510 --- /dev/null +++ b/auth/src/main/java/com/lighthouse/auth/di/OAuthQualifier.kt @@ -0,0 +1,7 @@ +package com.lighthouse.auth.di + +import javax.inject.Qualifier + +@Qualifier +@Retention(AnnotationRetention.BINARY) +annotation class GoogleOAuth diff --git a/auth/src/main/java/com/lighthouse/auth/mapper/AuthEncryptResultMapper.kt b/auth/src/main/java/com/lighthouse/auth/mapper/AuthEncryptResultMapper.kt deleted file mode 100644 index b11b66204..000000000 --- a/auth/src/main/java/com/lighthouse/auth/mapper/AuthEncryptResultMapper.kt +++ /dev/null @@ -1,8 +0,0 @@ -package com.lighthouse.auth.mapper - -import com.lighthouse.auth.model.AuthEncryptData -import com.lighthouse.beep.model.auth.EncryptData - -internal fun AuthEncryptData.toDomain(): EncryptData { - return EncryptData(data, iv) -} diff --git a/auth/src/main/java/com/lighthouse/auth/repository/AuthRepositoryImpl.kt b/auth/src/main/java/com/lighthouse/auth/repository/AuthRepositoryImpl.kt index 02d5ac18b..59fe43836 100644 --- a/auth/src/main/java/com/lighthouse/auth/repository/AuthRepositoryImpl.kt +++ b/auth/src/main/java/com/lighthouse/auth/repository/AuthRepositoryImpl.kt @@ -1,8 +1,13 @@ package com.lighthouse.auth.repository +import com.google.firebase.auth.AuthCredential import com.google.firebase.auth.FirebaseAuth.AuthStateListener +import com.google.firebase.auth.GoogleAuthProvider import com.google.firebase.auth.ktx.auth import com.google.firebase.ktx.Firebase +import com.lighthouse.auth.di.GoogleOAuth +import com.lighthouse.beep.model.auth.AuthProvider +import com.lighthouse.beep.model.auth.exception.InvalidUserException import com.lighthouse.domain.repository.auth.AuthRepository import kotlinx.coroutines.channels.awaitClose import kotlinx.coroutines.flow.Flow @@ -10,9 +15,11 @@ import kotlinx.coroutines.flow.callbackFlow import kotlinx.coroutines.flow.first import javax.inject.Inject -internal class AuthRepositoryImpl @Inject constructor() : AuthRepository { +internal class AuthRepositoryImpl @Inject constructor( + @GoogleOAuth private val googleOAuthRepository: OAuthRepository +) : AuthRepository { - override fun isGuest(): Flow = callbackFlow { + override fun isLogin(): Flow = callbackFlow { val authStateListener = AuthStateListener { trySend(it.currentUser == null) } @@ -22,17 +29,44 @@ internal class AuthRepositoryImpl @Inject constructor() : AuthRepository { } } + override fun isGuest(): Flow = callbackFlow { + val authStateListener = AuthStateListener { + trySend(it.currentUser == null || it.currentUser?.isAnonymous == true) + } + Firebase.auth.addAuthStateListener(authStateListener) + awaitClose { + Firebase.auth.removeAuthStateListener(authStateListener) + } + } + override fun getCurrentUserId(): String { - return Firebase.auth.currentUser?.uid ?: GUEST_ID + return Firebase.auth.currentUser?.uid ?: "" } - override fun signOut() { - Firebase.auth.signOut() + override suspend fun signIn(provider: AuthProvider, credential: AuthCredential): Result { + return runCatching { + when (provider) { + AuthProvider.GOOGLE -> googleOAuthRepository.signIn(credential) + } + } + } + + override suspend fun signOut(): Result { + val user = Firebase.auth.currentUser ?: return Result.failure(InvalidUserException()) + return runCatching { + when(user.providerId){ + GoogleAuthProvider.PROVIDER_ID -> googleOAuthRepository.signOut() + } + Firebase.auth.signOut() + } } override suspend fun withdrawal(): Result { - val user = Firebase.auth.currentUser ?: return Result.success(Unit) + val user = Firebase.auth.currentUser ?: return Result.failure(InvalidUserException()) return runCatching { + when(user.providerId){ + GoogleAuthProvider.PROVIDER_ID -> googleOAuthRepository.signOut() + } callbackFlow { user.delete().addOnCompleteListener { if (it.isSuccessful) { @@ -44,8 +78,4 @@ internal class AuthRepositoryImpl @Inject constructor() : AuthRepository { }.first() } } - - companion object { - private const val GUEST_ID = "Guest" - } } diff --git a/auth/src/main/java/com/lighthouse/auth/repository/OAuthRepository.kt b/auth/src/main/java/com/lighthouse/auth/repository/OAuthRepository.kt new file mode 100644 index 000000000..5e5b95c85 --- /dev/null +++ b/auth/src/main/java/com/lighthouse/auth/repository/OAuthRepository.kt @@ -0,0 +1,10 @@ +package com.lighthouse.auth.repository + +import com.google.firebase.auth.AuthCredential + +interface OAuthRepository { + + suspend fun signIn(credential: AuthCredential): Result + + suspend fun signOut(): Result +} \ No newline at end of file diff --git a/data/data-preference/build.gradle.kts b/data/data-preference/build.gradle.kts index d605107e9..0d9c7fd1b 100644 --- a/data/data-preference/build.gradle.kts +++ b/data/data-preference/build.gradle.kts @@ -17,6 +17,8 @@ dependencies { implementation(projects.commonAndroid) implementation(projects.data) + implementation(projects.ciphertool) + implementation(libs.androidX.datastore.preferences) implementation(libs.javax.inject) } diff --git a/data/data-preference/src/main/java/com/lighthouse/data/preference/repository/UserPreferenceRepositoryImpl.kt b/data/data-preference/src/main/java/com/lighthouse/data/preference/repository/UserPreferenceRepositoryImpl.kt index f2c55c5b1..81455fe6c 100644 --- a/data/data-preference/src/main/java/com/lighthouse/data/preference/repository/UserPreferenceRepositoryImpl.kt +++ b/data/data-preference/src/main/java/com/lighthouse/data/preference/repository/UserPreferenceRepositoryImpl.kt @@ -3,14 +3,13 @@ package com.lighthouse.data.preference.repository import androidx.datastore.core.DataStore import androidx.datastore.preferences.core.Preferences import androidx.datastore.preferences.core.edit -import androidx.datastore.preferences.core.stringPreferencesKey -import com.lighthouse.beep.model.auth.EncryptData import com.lighthouse.beep.model.user.SecurityOption import com.lighthouse.data.preference.exception.PrefNotFoundException import com.lighthouse.data.preference.ext.booleanKey import com.lighthouse.data.preference.ext.byteArrayKey import com.lighthouse.data.preference.ext.runCatchingPref import com.lighthouse.data.preference.ext.stringKey +import com.lighthouse.libs.ciphertool.EncryptData import com.lighthouse.repository.user.UserPreferenceRepository import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.distinctUntilChanged @@ -22,24 +21,6 @@ internal class UserPreferenceRepositoryImpl @Inject constructor( private val dataStore: DataStore ) : UserPreferenceRepository { - override suspend fun setLoginUserUid( - userId: String - ): Result = runCatchingPref { - val loginUserUidKey = stringPreferencesKey(KEY_NAME_LOGIN_USER_UID) - dataStore.edit { pref -> - pref[loginUserUidKey] = userId - } - } - - override fun getLoginUserUid(): Flow> { - val loginUserUidKey = stringPreferencesKey(KEY_NAME_LOGIN_USER_UID) - return dataStore.data.map { pref -> - runCatchingPref { - pref[loginUserUidKey] ?: "" - } - }.distinctUntilChanged() - } - override suspend fun setEncryptData( userId: String, encryptData: EncryptData @@ -176,7 +157,6 @@ internal class UserPreferenceRepositoryImpl @Inject constructor( } companion object { - private const val KEY_NAME_LOGIN_USER_UID = "LoginUserUid" private const val KEY_NAME_PIN_PASSWORD = "PinPassword" private const val KEY_NAME_IV = "IV" private const val KEY_NAME_SECURITY_OPTION = "SecurityOption" diff --git a/data/data/build.gradle.kts b/data/data/build.gradle.kts index d2c547ad2..c82dcb039 100644 --- a/data/data/build.gradle.kts +++ b/data/data/build.gradle.kts @@ -19,6 +19,8 @@ dependencies { implementation(projects.utilsLocation) implementation(projects.utilsRecognizer) + implementation(projects.ciphertool) + implementation(libs.androidX.paging.common.ktx) implementation(libs.kotlin.coroutine.core) diff --git a/data/data/src/main/java/com/lighthouse/repository/user/UserPreferenceRepository.kt b/data/data/src/main/java/com/lighthouse/repository/user/UserPreferenceRepository.kt index 8fd68459e..89aecca56 100644 --- a/data/data/src/main/java/com/lighthouse/repository/user/UserPreferenceRepository.kt +++ b/data/data/src/main/java/com/lighthouse/repository/user/UserPreferenceRepository.kt @@ -1,15 +1,11 @@ package com.lighthouse.repository.user -import com.lighthouse.beep.model.auth.EncryptData import com.lighthouse.beep.model.user.SecurityOption +import com.lighthouse.libs.ciphertool.EncryptData import kotlinx.coroutines.flow.Flow interface UserPreferenceRepository { - fun getLoginUserUid(): Flow> - - suspend fun setLoginUserUid(userId: String): Result - suspend fun setEncryptData(userId: String, encryptData: EncryptData): Result fun getEncryptData(userId: String): Flow> diff --git a/data/data/src/main/java/com/lighthouse/repository/user/UserRepositoryImpl.kt b/data/data/src/main/java/com/lighthouse/repository/user/UserRepositoryImpl.kt index 05e7f03b4..0f6e6ebca 100644 --- a/data/data/src/main/java/com/lighthouse/repository/user/UserRepositoryImpl.kt +++ b/data/data/src/main/java/com/lighthouse/repository/user/UserRepositoryImpl.kt @@ -1,56 +1,30 @@ package com.lighthouse.repository.user -import com.lighthouse.beep.model.auth.EncryptData import com.lighthouse.beep.model.user.SecurityOption import com.lighthouse.domain.repository.user.UserRepository +import com.lighthouse.libs.ciphertool.CipherTool import kotlinx.coroutines.flow.Flow -import kotlinx.coroutines.flow.map import javax.inject.Inject +import kotlinx.coroutines.flow.map internal class UserRepositoryImpl @Inject constructor( private val userPreferenceRepository: UserPreferenceRepository ) : UserRepository { - override fun isLogin(): Flow { - return userPreferenceRepository.getLoginUserUid().map { - it.getOrDefault("") != "" + override suspend fun setPinPassword(userId: String, newPin: String): Result = + runCatching { + val encryptData = CipherTool.encrypt(SecurityOption.PIN.name, newPin) + userPreferenceRepository.setEncryptData(userId, encryptData).getOrThrow() } - } - - override suspend fun login(userId: String): Result { - return userPreferenceRepository.setLoginUserUid(userId) - } - - override suspend fun signOut(): Result { - return userPreferenceRepository.setLoginUserUid("") - } - // override fun getUserId(): String { -// return authRepository.getCurrentUserId() -// } -// -// override fun isGuest(): Flow { -// return authRepository.isGuest() -// } - -// override suspend fun setPinPassword(userId: String, pinPassword: String): Result = runCatching { -// val userId = authRepository.getCurrentUserId() -// val encryptData = cipherTool.encrypt(SecurityOption.PIN.name, pinPassword).getOrThrow() -// userPreferenceRepository.setEncryptData(userId, encryptData).getOrThrow() -// } -// -// override suspend fun confirmPinPassword(userId: String, pinPassword: String): Result = runCatching { -// val encryptData = userPreferenceRepository.getEncryptData(userId).getOrThrow() -// val decryptData = cipherTool.decrypt(SecurityOption.PIN.name, encryptData).getOrThrow() -// pinPassword == decryptData -// } - - override suspend fun setPinPassword(userId: String, encryptData: EncryptData): Result { - return userPreferenceRepository.setEncryptData(userId, encryptData) - } - - override fun getPinPassword(userId: String): Flow> { - return userPreferenceRepository.getEncryptData(userId) + override fun confirmPinPassword(userId: String, pin: String): Flow> { + return userPreferenceRepository.getEncryptData(userId).map { + runCatching { + val encryptPin = it.getOrThrow() + val decryptData = CipherTool.decrypt(SecurityOption.PIN.name, encryptPin) + decryptData == pin + } + } } override suspend fun setSecurityOption( @@ -80,14 +54,11 @@ internal class UserRepositoryImpl @Inject constructor( return userPreferenceRepository.getFilterExpired(userId) } - override suspend fun transferData(userId: String, newUserId: String): Result = - runCatching { - userPreferenceRepository.transferData(userId, newUserId).getOrThrow() - userPreferenceRepository.setLoginUserUid(newUserId).getOrThrow() - } + override suspend fun transferData(userId: String, newUserId: String): Result { + return userPreferenceRepository.transferData(userId, newUserId) + } - override suspend fun withdrawal(userId: String): Result = runCatching { - userPreferenceRepository.withdrawal(userId).getOrThrow() - userPreferenceRepository.setLoginUserUid("").getOrThrow() + override suspend fun withdrawal(userId: String): Result { + return userPreferenceRepository.withdrawal(userId) } } diff --git a/domain/src/main/java/com/lighthouse/domain/repository/auth/AuthRepository.kt b/domain/src/main/java/com/lighthouse/domain/repository/auth/AuthRepository.kt index 96f5c706b..75fcac542 100644 --- a/domain/src/main/java/com/lighthouse/domain/repository/auth/AuthRepository.kt +++ b/domain/src/main/java/com/lighthouse/domain/repository/auth/AuthRepository.kt @@ -1,14 +1,20 @@ package com.lighthouse.domain.repository.auth +import com.google.firebase.auth.AuthCredential +import com.lighthouse.beep.model.auth.AuthProvider import kotlinx.coroutines.flow.Flow interface AuthRepository { + fun isLogin(): Flow + fun isGuest(): Flow fun getCurrentUserId(): String - fun signOut() + suspend fun signIn(provider: AuthProvider, credential: AuthCredential): Result + + suspend fun signOut(): Result suspend fun withdrawal(): Result } diff --git a/domain/src/main/java/com/lighthouse/domain/repository/user/UserRepository.kt b/domain/src/main/java/com/lighthouse/domain/repository/user/UserRepository.kt index 2d3196e93..c751e2b98 100644 --- a/domain/src/main/java/com/lighthouse/domain/repository/user/UserRepository.kt +++ b/domain/src/main/java/com/lighthouse/domain/repository/user/UserRepository.kt @@ -1,17 +1,12 @@ package com.lighthouse.domain.repository.user -import com.lighthouse.beep.model.auth.EncryptData import com.lighthouse.beep.model.user.SecurityOption import kotlinx.coroutines.flow.Flow interface UserRepository { - fun isLogin(): Flow - suspend fun login(userId: String): Result - suspend fun signOut(): Result - - suspend fun setPinPassword(userId: String, encryptData: EncryptData): Result - fun getPinPassword(userId: String): Flow> + suspend fun setPinPassword(userId: String, newPin: String): Result + fun confirmPinPassword(userId: String, pin: String): Flow> suspend fun setSecurityOption(userId: String, securityOption: SecurityOption): Result fun getSecurityOption(userId: String): Flow> diff --git a/domain/src/main/java/com/lighthouse/domain/usecase/gifticon/edit/GetGifticonForUpdateUseCase.kt b/domain/src/main/java/com/lighthouse/domain/usecase/gifticon/edit/GetGifticonForUpdateUseCase.kt deleted file mode 100644 index 27a7a9d8d..000000000 --- a/domain/src/main/java/com/lighthouse/domain/usecase/gifticon/edit/GetGifticonForUpdateUseCase.kt +++ /dev/null @@ -1,19 +0,0 @@ -package com.lighthouse.domain.usecase.gifticon.edit - -import com.lighthouse.beep.model.gifticon.GifticonWithCrop -import com.lighthouse.domain.repository.auth.AuthRepository -import com.lighthouse.domain.repository.gifticon.GifticonSearchRepository -import javax.inject.Inject - -class GetGifticonForUpdateUseCase @Inject constructor( - private val authRepository: AuthRepository, - private val gifticonRepository: GifticonSearchRepository -) { - - suspend operator fun invoke(gifticonId: String): Result { - return gifticonRepository.getGifticonCrop( - authRepository.getCurrentUserId(), - gifticonId - ) - } -} diff --git a/domain/src/main/java/com/lighthouse/domain/usecase/gifticon/edit/ModifyGifticonUseCase.kt b/domain/src/main/java/com/lighthouse/domain/usecase/gifticon/edit/ModifyGifticonUseCase.kt index 672ca3601..4ed5608be 100644 --- a/domain/src/main/java/com/lighthouse/domain/usecase/gifticon/edit/ModifyGifticonUseCase.kt +++ b/domain/src/main/java/com/lighthouse/domain/usecase/gifticon/edit/ModifyGifticonUseCase.kt @@ -1,6 +1,6 @@ package com.lighthouse.domain.usecase.gifticon.edit -import com.lighthouse.beep.model.exception.auth.AuthenticationException +import com.lighthouse.beep.model.auth.exception.AuthenticationException import com.lighthouse.beep.model.gifticon.GifticonForUpdate import com.lighthouse.domain.repository.auth.AuthRepository import com.lighthouse.domain.repository.gifticon.GifticonEditRepository diff --git a/domain/src/main/java/com/lighthouse/domain/usecase/user/GetUserIdUseCase.kt b/domain/src/main/java/com/lighthouse/domain/usecase/user/GetUserIdUseCase.kt index 729998ab9..2208bd9d6 100644 --- a/domain/src/main/java/com/lighthouse/domain/usecase/user/GetUserIdUseCase.kt +++ b/domain/src/main/java/com/lighthouse/domain/usecase/user/GetUserIdUseCase.kt @@ -7,7 +7,7 @@ class GetUserIdUseCase @Inject constructor( private val authRepository: AuthRepository ) { - operator fun invoke(): String { + operator fun invoke(): String? { return authRepository.getCurrentUserId() } } diff --git a/domain/src/main/java/com/lighthouse/domain/usecase/user/IsLoginUserUseCase.kt b/domain/src/main/java/com/lighthouse/domain/usecase/user/IsLoginUserUseCase.kt index c98b591db..733b9f928 100644 --- a/domain/src/main/java/com/lighthouse/domain/usecase/user/IsLoginUserUseCase.kt +++ b/domain/src/main/java/com/lighthouse/domain/usecase/user/IsLoginUserUseCase.kt @@ -1,14 +1,15 @@ package com.lighthouse.domain.usecase.user +import com.lighthouse.domain.repository.auth.AuthRepository import com.lighthouse.domain.repository.user.UserRepository import kotlinx.coroutines.flow.Flow import javax.inject.Inject class IsLoginUserUseCase @Inject constructor( - private val userRepository: UserRepository + private val authRepository: AuthRepository ) { operator fun invoke(): Flow { - return userRepository.isLogin() + return authRepository.isLogin() } } diff --git a/domain/src/main/java/com/lighthouse/domain/usecase/user/LoginUseCase.kt b/domain/src/main/java/com/lighthouse/domain/usecase/user/LoginUseCase.kt deleted file mode 100644 index 03993bf77..000000000 --- a/domain/src/main/java/com/lighthouse/domain/usecase/user/LoginUseCase.kt +++ /dev/null @@ -1,16 +0,0 @@ -package com.lighthouse.domain.usecase.user - -import com.lighthouse.domain.repository.auth.AuthRepository -import com.lighthouse.domain.repository.user.UserRepository -import javax.inject.Inject - -class LoginUseCase @Inject constructor( - private val authRepository: AuthRepository, - private val userRepository: UserRepository -) { - - suspend operator fun invoke(): Result { - val userId = authRepository.getCurrentUserId() - return userRepository.login(userId) - } -} diff --git a/domain/src/main/java/com/lighthouse/domain/usecase/user/SignInUseCase.kt b/domain/src/main/java/com/lighthouse/domain/usecase/user/SignInUseCase.kt new file mode 100644 index 000000000..ce30089c2 --- /dev/null +++ b/domain/src/main/java/com/lighthouse/domain/usecase/user/SignInUseCase.kt @@ -0,0 +1,15 @@ +package com.lighthouse.domain.usecase.user + +import com.google.firebase.auth.AuthCredential +import com.lighthouse.beep.model.auth.AuthProvider +import com.lighthouse.domain.repository.auth.AuthRepository +import javax.inject.Inject + +class SignInUseCase @Inject constructor( + private val authRepository: AuthRepository +) { + + suspend operator fun invoke(provider: AuthProvider, credential: AuthCredential): Result { + return authRepository.signIn(provider, credential) + } +} \ No newline at end of file diff --git a/domain/src/main/java/com/lighthouse/domain/usecase/user/SignOutUseCase.kt b/domain/src/main/java/com/lighthouse/domain/usecase/user/SignOutUseCase.kt index 186d72796..42a7df619 100644 --- a/domain/src/main/java/com/lighthouse/domain/usecase/user/SignOutUseCase.kt +++ b/domain/src/main/java/com/lighthouse/domain/usecase/user/SignOutUseCase.kt @@ -1,16 +1,13 @@ package com.lighthouse.domain.usecase.user import com.lighthouse.domain.repository.auth.AuthRepository -import com.lighthouse.domain.repository.user.UserRepository import javax.inject.Inject class SignOutUseCase @Inject constructor( - private val authRepository: AuthRepository, - private val userRepository: UserRepository + private val authRepository: AuthRepository ) { - suspend operator fun invoke(): Result = runCatching { - authRepository.signOut() - userRepository.signOut().getOrThrow() + suspend operator fun invoke(): Result { + return authRepository.signOut() } } diff --git a/domain/src/main/java/com/lighthouse/domain/usecase/user/TransferDataUseCase.kt b/domain/src/main/java/com/lighthouse/domain/usecase/user/TransferDataUseCase.kt index 0f03b2d3e..1349539c3 100644 --- a/domain/src/main/java/com/lighthouse/domain/usecase/user/TransferDataUseCase.kt +++ b/domain/src/main/java/com/lighthouse/domain/usecase/user/TransferDataUseCase.kt @@ -1,5 +1,6 @@ package com.lighthouse.domain.usecase.user +import com.lighthouse.beep.model.auth.exception.InvalidUserException import com.lighthouse.domain.repository.auth.AuthRepository import com.lighthouse.domain.repository.user.UserRepository import javax.inject.Inject @@ -10,7 +11,9 @@ class TransferDataUseCase @Inject constructor( ) { suspend operator fun invoke(oldUserId: String): Result { - val newUserId = authRepository.getCurrentUserId() - return userRepository.transferData(oldUserId, newUserId) + return runCatching { + val newUserId = authRepository.getCurrentUserId() + userRepository.transferData(oldUserId, newUserId).getOrThrow() + } } } diff --git a/domain/src/main/java/com/lighthouse/domain/usecase/user/WithdrawalUseCase.kt b/domain/src/main/java/com/lighthouse/domain/usecase/user/WithdrawalUseCase.kt index 91a07d0fe..0385bbedd 100644 --- a/domain/src/main/java/com/lighthouse/domain/usecase/user/WithdrawalUseCase.kt +++ b/domain/src/main/java/com/lighthouse/domain/usecase/user/WithdrawalUseCase.kt @@ -1,5 +1,6 @@ package com.lighthouse.domain.usecase.user +import com.lighthouse.beep.model.auth.exception.InvalidUserException import com.lighthouse.domain.repository.auth.AuthRepository import com.lighthouse.domain.repository.user.UserRepository import javax.inject.Inject @@ -10,9 +11,9 @@ class WithdrawalUseCase @Inject constructor( ) { suspend operator fun invoke(): Result = runCatching { - val userId = authRepository.getCurrentUserId() + val userId = authRepository.getCurrentUserId() ?: throw InvalidUserException() authRepository.withdrawal().getOrThrow() - authRepository.signOut() + authRepository.signOut().getOrThrow() userRepository.withdrawal(userId).getOrThrow() } } diff --git a/features/ui-common/build.gradle.kts b/features/ui-common/build.gradle.kts index 2e7bc0b80..87bdf9aaa 100644 --- a/features/ui-common/build.gradle.kts +++ b/features/ui-common/build.gradle.kts @@ -25,4 +25,7 @@ dependencies { implementation(libs.material) implementation(libs.coil) + + implementation(platform(libs.firebase.bom)) + implementation(libs.firebase.auth.ktx) } diff --git a/auth-google/src/main/java/com/lighthouse/auth/google/ui/GoogleAuthViewModel.kt b/features/ui-common/src/main/java/com/lighthouse/features/common/ui/AuthViewModel.kt similarity index 56% rename from auth-google/src/main/java/com/lighthouse/auth/google/ui/GoogleAuthViewModel.kt rename to features/ui-common/src/main/java/com/lighthouse/features/common/ui/AuthViewModel.kt index 53c9dac97..976b7f6e1 100644 --- a/auth-google/src/main/java/com/lighthouse/auth/google/ui/GoogleAuthViewModel.kt +++ b/features/ui-common/src/main/java/com/lighthouse/features/common/ui/AuthViewModel.kt @@ -1,65 +1,55 @@ -package com.lighthouse.auth.google.ui +package com.lighthouse.features.common.ui import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope -import com.lighthouse.auth.google.R -import com.lighthouse.auth.google.exception.FailedConnectException -import com.lighthouse.auth.google.model.GoogleAuthEvent -import com.lighthouse.beep.model.exception.auth.FailedSaveLoginUserException +import com.google.firebase.auth.AuthCredential +import com.lighthouse.beep.model.auth.AuthProvider +import com.lighthouse.beep.model.auth.exception.FailedConnectException +import com.lighthouse.beep.model.auth.exception.FailedSaveLoginUserException import com.lighthouse.core.android.utils.resource.UIText import com.lighthouse.core.utils.flow.MutableEventFlow import com.lighthouse.core.utils.flow.asEventFlow -import com.lighthouse.domain.usecase.user.GetUserIdUseCase -import com.lighthouse.domain.usecase.user.LoginUseCase +import com.lighthouse.domain.usecase.user.SignInUseCase import com.lighthouse.domain.usecase.user.SignOutUseCase -import com.lighthouse.domain.usecase.user.TransferDataUseCase import com.lighthouse.domain.usecase.user.WithdrawalUseCase +import com.lighthouse.features.common.R +import com.lighthouse.features.common.model.MessageEvent import dagger.hilt.android.lifecycle.HiltViewModel +import java.lang.Exception +import javax.inject.Inject import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.asStateFlow import kotlinx.coroutines.launch -import java.lang.Exception -import javax.inject.Inject @HiltViewModel -class GoogleAuthViewModel @Inject constructor( - private val getUserIdUseCase: GetUserIdUseCase, - private val loginUseCase: LoginUseCase, - private val transferDataUseCase: TransferDataUseCase, +class AuthViewModel @Inject constructor( + private val signInUseCase: SignInUseCase, private val signOutUseCase: SignOutUseCase, private val withdrawalUseCase: WithdrawalUseCase ) : ViewModel() { - private val _eventFlow = MutableEventFlow() + private val _eventFlow = MutableEventFlow() val eventFlow = _eventFlow.asEventFlow() - private val _signInLoading = MutableStateFlow(false) - val signInLoading = _signInLoading.asStateFlow() - - fun getUserId() = getUserIdUseCase() - - fun login() { - viewModelScope.launch { - finishSignIn(loginUseCase().exceptionOrNull()) - } - } + private val _loginLoading = MutableStateFlow(false) + val loginLoading = _loginLoading.asStateFlow() - fun transferData(oldUserId: String) { + fun login(provider: AuthProvider, credential: AuthCredential) { viewModelScope.launch { - finishSignIn(transferDataUseCase(oldUserId).exceptionOrNull()) + endLogin(signInUseCase(provider, credential).exceptionOrNull()) } } - fun startSignIn() { - _signInLoading.value = true + fun startLogin() { + _loginLoading.value = true } - fun finishSignIn(throwable: Throwable? = null) { - finishSignIn(throwable as? Exception) + fun endLogin(throwable: Throwable? = null) { + endLogin(throwable as? Exception) } - private fun finishSignIn(exception: Exception? = null) { - _signInLoading.value = false + fun endLogin(exception: Exception? = null) { + _loginLoading.value = false viewModelScope.launch { val stringRes = when (exception) { null -> R.string.login_success @@ -67,7 +57,7 @@ class GoogleAuthViewModel @Inject constructor( is FailedConnectException -> R.string.google_connect_fail else -> R.string.error_unknown } - _eventFlow.emit(GoogleAuthEvent.SnackBar(UIText.StringResource(stringRes))) + _eventFlow.emit(MessageEvent.SnackBar(UIText.StringResource(stringRes))) } } @@ -76,7 +66,7 @@ class GoogleAuthViewModel @Inject constructor( val exception = signOutUseCase().exceptionOrNull() if (exception != null) { _eventFlow.emit( - GoogleAuthEvent.SnackBar( + MessageEvent.SnackBar( UIText.StringResource(R.string.error_sign_out) ) ) @@ -89,11 +79,11 @@ class GoogleAuthViewModel @Inject constructor( val exception = withdrawalUseCase().exceptionOrNull() if (exception != null) { _eventFlow.emit( - GoogleAuthEvent.SnackBar( + MessageEvent.SnackBar( UIText.StringResource(R.string.error_withdrawal) ) ) } } } -} +} \ No newline at end of file diff --git a/features/ui-common/src/main/java/com/lighthouse/features/common/ui/MessageViewModel.kt b/features/ui-common/src/main/java/com/lighthouse/features/common/ui/MessageViewModel.kt index 3ba29a7c9..f3570033d 100644 --- a/features/ui-common/src/main/java/com/lighthouse/features/common/ui/MessageViewModel.kt +++ b/features/ui-common/src/main/java/com/lighthouse/features/common/ui/MessageViewModel.kt @@ -18,6 +18,12 @@ class MessageViewModel @Inject constructor() : ViewModel() { val messageFlow = _messageFlow.asEventFlow() + fun sendMessage(event: MessageEvent) { + viewModelScope.launch { + _messageFlow.emit(event) + } + } + fun sendSnackBar(@StringRes resId: Int) { viewModelScope.launch { _messageFlow.emit(MessageEvent.SnackBar(UIText.StringResource(resId))) diff --git a/features/ui-common/src/main/res/values/strings.xml b/features/ui-common/src/main/res/values/strings.xml index 6dd394819..daaed2248 100644 --- a/features/ui-common/src/main/res/values/strings.xml +++ b/features/ui-common/src/main/res/values/strings.xml @@ -9,4 +9,15 @@ 확인 + + 구글 로그인 실패 + 구글 연결 실패 + 로그인 성공 + 로그인 실패 + + 로그인 정보를 저장하는데 실패 했습니다. + 알 수 없는 오류 입니다. + + 로그아웃에 실패 했습니다. + 회원 탈퇴에 실패 했습니다. \ No newline at end of file diff --git a/features/ui-intro/build.gradle.kts b/features/ui-intro/build.gradle.kts index 80a144811..c5df4dd20 100644 --- a/features/ui-intro/build.gradle.kts +++ b/features/ui-intro/build.gradle.kts @@ -27,5 +27,8 @@ dependencies { implementation(libs.material) + implementation(platform(libs.firebase.bom)) + implementation(libs.firebase.auth.ktx) + implementation(libs.timber) } diff --git a/features/ui-intro/src/main/java/com/lighthouse/features/intro/ui/IntroFragment.kt b/features/ui-intro/src/main/java/com/lighthouse/features/intro/ui/IntroFragment.kt index c15989a5c..3bfa0194b 100644 --- a/features/ui-intro/src/main/java/com/lighthouse/features/intro/ui/IntroFragment.kt +++ b/features/ui-intro/src/main/java/com/lighthouse/features/intro/ui/IntroFragment.kt @@ -8,13 +8,13 @@ import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels import androidx.fragment.app.viewModels import androidx.lifecycle.lifecycleScope -import com.lighthouse.auth.google.model.GoogleAuthEvent import com.lighthouse.auth.google.repository.GoogleClient -import com.lighthouse.auth.google.ui.GoogleAuthViewModel +import com.lighthouse.beep.model.auth.AuthProvider import com.lighthouse.features.common.binding.viewBindings import com.lighthouse.features.common.dialog.progress.ProgressDialog import com.lighthouse.features.common.ext.repeatOnStarted import com.lighthouse.features.common.ext.show +import com.lighthouse.features.common.ui.AuthViewModel import com.lighthouse.features.common.ui.MessageViewModel import com.lighthouse.features.common.utils.throttle.onThrottleClick import com.lighthouse.features.intro.R @@ -30,21 +30,21 @@ class IntroFragment : Fragment(R.layout.fragment_intro) { private val messageViewModel: MessageViewModel by activityViewModels() - private val googleAuthViewModel: GoogleAuthViewModel by viewModels() + private val authViewModel: AuthViewModel by viewModels() @Inject lateinit var googleClient: GoogleClient private var progressDialog: ProgressDialog? = null - private val loginLauncher = + private val googleLoginLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> lifecycleScope.launch { - val exception = googleClient.signIn(result).exceptionOrNull() - if (exception != null) { - googleAuthViewModel.finishSignIn(exception) - } else { - googleAuthViewModel.login() + try { + val credential = googleClient.getAuthCredential(result).getOrThrow() + authViewModel.login(AuthProvider.GOOGLE, credential) + } catch (e: Exception) { + authViewModel.endLogin(e) } } } @@ -66,17 +66,15 @@ class IntroFragment : Fragment(R.layout.fragment_intro) { private fun setUpGoogleAuthEvent() { repeatOnStarted { - googleAuthViewModel.eventFlow.collect { event -> - when (event) { - is GoogleAuthEvent.SnackBar -> messageViewModel.sendToast(event.text) - } + authViewModel.eventFlow.collect { event -> + messageViewModel.sendMessage(event) } } } private fun setUpSignInLoading() { repeatOnStarted { - googleAuthViewModel.signInLoading.collect { loading -> + authViewModel.loginLoading.collect { loading -> if (loading) { if (progressDialog?.isAdded == true) { progressDialog?.dismiss() @@ -92,15 +90,15 @@ class IntroFragment : Fragment(R.layout.fragment_intro) { private fun setUpBtnGoogleLogin() { binding.btnGoogleLogin.onThrottleClick { - googleAuthViewModel.startSignIn() - loginLauncher.launch(googleClient.signInIntent()) + authViewModel.startLogin() + googleLoginLauncher.launch(googleClient.signInIntent()) } } private fun setUpTvGuestSignIn() { binding.tvGuestSignin.onThrottleClick { - googleAuthViewModel.startSignIn() - googleAuthViewModel.login() +// googleAuthViewModel.startSignIn() +// googleAuthViewModel.login() } } } diff --git a/features/ui-setting/build.gradle.kts b/features/ui-setting/build.gradle.kts index 4f081871a..e7016f9bd 100644 --- a/features/ui-setting/build.gradle.kts +++ b/features/ui-setting/build.gradle.kts @@ -28,5 +28,8 @@ dependencies { implementation(libs.material) + implementation(platform(libs.firebase.bom)) + implementation(libs.firebase.auth.ktx) + implementation(libs.timber) } diff --git a/features/ui-setting/src/main/java/com/lighthouse/features/setting/ui/SettingFragment.kt b/features/ui-setting/src/main/java/com/lighthouse/features/setting/ui/SettingFragment.kt index 0e51e7fc5..4548ac78e 100644 --- a/features/ui-setting/src/main/java/com/lighthouse/features/setting/ui/SettingFragment.kt +++ b/features/ui-setting/src/main/java/com/lighthouse/features/setting/ui/SettingFragment.kt @@ -7,14 +7,14 @@ import androidx.fragment.app.Fragment import androidx.fragment.app.activityViewModels import androidx.fragment.app.viewModels import androidx.lifecycle.lifecycleScope -import com.lighthouse.auth.google.model.GoogleAuthEvent import com.lighthouse.auth.google.repository.GoogleClient -import com.lighthouse.auth.google.ui.GoogleAuthViewModel +import com.lighthouse.beep.model.auth.AuthProvider import com.lighthouse.core.android.utils.permission.LocationPermissionManager import com.lighthouse.features.common.binding.viewBindings import com.lighthouse.features.common.dialog.progress.ProgressDialog import com.lighthouse.features.common.ext.repeatOnStarted import com.lighthouse.features.common.ext.show +import com.lighthouse.features.common.ui.AuthViewModel import com.lighthouse.features.common.ui.MessageViewModel import com.lighthouse.features.setting.R import com.lighthouse.features.setting.adapter.SettingAdapter @@ -46,21 +46,21 @@ class SettingFragment : Fragment(R.layout.fragment_setting) { } ) - private val googleAuthViewModel: GoogleAuthViewModel by viewModels() + private val authViewModel: AuthViewModel by viewModels() @Inject lateinit var googleClient: GoogleClient private var progressDialog: ProgressDialog? = null - private val loginLauncher = + private val googleLoginLauncher = registerForActivityResult(ActivityResultContracts.StartActivityForResult()) { result -> lifecycleScope.launch { - val exception = googleClient.signIn(result).exceptionOrNull() - if (exception != null) { - googleAuthViewModel.finishSignIn(exception) - } else { - googleAuthViewModel.login() + try { + val credential = googleClient.getAuthCredential(result).getOrThrow() + authViewModel.login(AuthProvider.GOOGLE, credential) + } catch (e: Exception) { + authViewModel.endLogin(e) } } } @@ -111,18 +111,15 @@ class SettingFragment : Fragment(R.layout.fragment_setting) { private fun setUpGoogleAuthEvent() { viewLifecycleOwner.repeatOnStarted { - googleAuthViewModel.eventFlow.collect { event -> - when (event) { - is GoogleAuthEvent.SnackBar -> - messageViewModel.sendSnackBar(event.text) - } + authViewModel.eventFlow.collect { event -> + messageViewModel.sendMessage(event) } } } private fun setUpSignInLoading() { repeatOnStarted { - googleAuthViewModel.signInLoading.collect { loading -> + authViewModel.loginLoading.collect { loading -> if (loading) { if (progressDialog?.isAdded == true) { progressDialog?.dismiss() @@ -178,30 +175,16 @@ class SettingFragment : Fragment(R.layout.fragment_setting) { } private fun signIn() { - googleAuthViewModel.startSignIn() - loginLauncher.launch(googleClient.signInIntent()) + authViewModel.startLogin() + googleLoginLauncher.launch(googleClient.signInIntent()) } private fun signOut() { - lifecycleScope.launch { - val exception = googleClient.signOut().exceptionOrNull() - if (exception != null) { - messageViewModel.sendSnackBar(R.string.error_sign_out_google_client) - } else { - googleAuthViewModel.signOut() - } - } + authViewModel.signOut() } private fun withdrawal() { - lifecycleScope.launch { - val exception = googleClient.signOut().exceptionOrNull() - if (exception != null) { - messageViewModel.sendSnackBar(R.string.error_sign_out_google_client) - } else { - googleAuthViewModel.withdrawal() - } - } + authViewModel.withdrawal() } private fun setUpMenuOnCheckedChange(menu: SettingMenu, isChecked: Boolean) { diff --git a/libs/ciphertool/.gitignore b/libs/ciphertool/.gitignore new file mode 100644 index 000000000..42afabfd2 --- /dev/null +++ b/libs/ciphertool/.gitignore @@ -0,0 +1 @@ +/build \ No newline at end of file diff --git a/model/src/main/java/com/lighthouse/beep/model/auth/AuthProvider.kt b/model/src/main/java/com/lighthouse/beep/model/auth/AuthProvider.kt new file mode 100644 index 000000000..8b6849449 --- /dev/null +++ b/model/src/main/java/com/lighthouse/beep/model/auth/AuthProvider.kt @@ -0,0 +1,6 @@ +package com.lighthouse.beep.model.auth + +enum class AuthProvider { + + GOOGLE +} \ No newline at end of file diff --git a/model/src/main/java/com/lighthouse/beep/model/exception/auth/AuthenticationException.kt b/model/src/main/java/com/lighthouse/beep/model/auth/exception/AuthenticationException.kt similarity index 68% rename from model/src/main/java/com/lighthouse/beep/model/exception/auth/AuthenticationException.kt rename to model/src/main/java/com/lighthouse/beep/model/auth/exception/AuthenticationException.kt index 0bbf8e89c..fde87f6c3 100644 --- a/model/src/main/java/com/lighthouse/beep/model/exception/auth/AuthenticationException.kt +++ b/model/src/main/java/com/lighthouse/beep/model/auth/exception/AuthenticationException.kt @@ -1,3 +1,3 @@ -package com.lighthouse.beep.model.exception.auth +package com.lighthouse.beep.model.auth.exception class AuthenticationException(message: String = "인증에 실패 하였습니다.") : Exception(message) diff --git a/auth-google/src/main/java/com/lighthouse/auth/google/exception/FailedConnectException.kt b/model/src/main/java/com/lighthouse/beep/model/auth/exception/FailedConnectException.kt similarity index 70% rename from auth-google/src/main/java/com/lighthouse/auth/google/exception/FailedConnectException.kt rename to model/src/main/java/com/lighthouse/beep/model/auth/exception/FailedConnectException.kt index d0ac09046..5015c69c4 100644 --- a/auth-google/src/main/java/com/lighthouse/auth/google/exception/FailedConnectException.kt +++ b/model/src/main/java/com/lighthouse/beep/model/auth/exception/FailedConnectException.kt @@ -1,4 +1,4 @@ -package com.lighthouse.auth.google.exception +package com.lighthouse.beep.model.auth.exception class FailedConnectException( message: String = "서버와 연결을 실패 했습니다." diff --git a/model/src/main/java/com/lighthouse/beep/model/exception/auth/FailedSaveLoginUserException.kt b/model/src/main/java/com/lighthouse/beep/model/auth/exception/FailedSaveLoginUserException.kt similarity index 72% rename from model/src/main/java/com/lighthouse/beep/model/exception/auth/FailedSaveLoginUserException.kt rename to model/src/main/java/com/lighthouse/beep/model/auth/exception/FailedSaveLoginUserException.kt index b2fc73e80..884b82590 100644 --- a/model/src/main/java/com/lighthouse/beep/model/exception/auth/FailedSaveLoginUserException.kt +++ b/model/src/main/java/com/lighthouse/beep/model/auth/exception/FailedSaveLoginUserException.kt @@ -1,4 +1,4 @@ -package com.lighthouse.beep.model.exception.auth +package com.lighthouse.beep.model.auth.exception class FailedSaveLoginUserException( message: String = "로그인 정보 저장에 실패 했습니다." diff --git a/model/src/main/java/com/lighthouse/beep/model/exception/auth/InvalidUserException.kt b/model/src/main/java/com/lighthouse/beep/model/auth/exception/InvalidUserException.kt similarity index 68% rename from model/src/main/java/com/lighthouse/beep/model/exception/auth/InvalidUserException.kt rename to model/src/main/java/com/lighthouse/beep/model/auth/exception/InvalidUserException.kt index b4c928a41..33dd863b6 100644 --- a/model/src/main/java/com/lighthouse/beep/model/exception/auth/InvalidUserException.kt +++ b/model/src/main/java/com/lighthouse/beep/model/auth/exception/InvalidUserException.kt @@ -1,3 +1,3 @@ -package com.lighthouse.beep.model.exception.auth +package com.lighthouse.beep.model.auth.exception class InvalidUserException(message: String? = "유효하지 않은 유저입니다.") : Exception(message)