diff --git a/app/build.gradle b/app/build.gradle index 0b1e90a..c3cffec 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -94,7 +94,8 @@ dependencies { implementation platform('com.google.firebase:firebase-bom:29.2.0') implementation 'com.google.firebase:firebase-analytics-ktx' implementation 'com.google.firebase:firebase-crashlytics-ktx' - implementation 'com.google.firebase:firebase-auth-ktx' + + // Google Login implementation 'com.google.android.gms:play-services-auth:20.1.0' // Hilt diff --git a/app/src/main/java/com/moyerun/moyeorun_android/login/LoginEvent.kt b/app/src/main/java/com/moyerun/moyeorun_android/login/LoginEvent.kt deleted file mode 100644 index a36af17..0000000 --- a/app/src/main/java/com/moyerun/moyeorun_android/login/LoginEvent.kt +++ /dev/null @@ -1,5 +0,0 @@ -package com.moyerun.moyeorun_android.login - -enum class LoginEvent { - Success, NewUser, Error -} \ No newline at end of file diff --git a/app/src/main/java/com/moyerun/moyeorun_android/login/LoginModule.kt b/app/src/main/java/com/moyerun/moyeorun_android/login/LoginModule.kt index 7e0814b..32db6eb 100644 --- a/app/src/main/java/com/moyerun/moyeorun_android/login/LoginModule.kt +++ b/app/src/main/java/com/moyerun/moyeorun_android/login/LoginModule.kt @@ -1,5 +1,6 @@ package com.moyerun.moyeorun_android.login +import com.moyerun.moyeorun_android.login.data.LoginRepository import dagger.Module import dagger.Provides import dagger.hilt.InstallIn diff --git a/app/src/main/java/com/moyerun/moyeorun_android/login/LoginRepository.kt b/app/src/main/java/com/moyerun/moyeorun_android/login/LoginRepository.kt deleted file mode 100644 index 6aaaf62..0000000 --- a/app/src/main/java/com/moyerun/moyeorun_android/login/LoginRepository.kt +++ /dev/null @@ -1,59 +0,0 @@ -package com.moyerun.moyeorun_android.login - -import com.google.firebase.auth.FirebaseUser -import com.google.firebase.auth.GoogleAuthProvider -import com.google.firebase.auth.ktx.auth -import com.google.firebase.ktx.Firebase -import kotlinx.coroutines.* -import java.lang.IllegalStateException -import kotlin.coroutines.resume - -class LoginRepository( - private val coroutineDispatcher: CoroutineDispatcher = Dispatchers.IO -) { - - // Todo: 모여런 서버 붙일 때 반환 타입 조정 - suspend fun signIn(idToken: String): ApiResponse { - val firebaseSignInResult = withContext(coroutineDispatcher) { signInWithFirebaseCredential(idToken) } - if (firebaseSignInResult is ApiResponse.Failure) { - return firebaseSignInResult - } - val moyeorunSignInResult = withContext(coroutineDispatcher) { signInWithMoyeoRun(idToken) } - if (firebaseSignInResult is ApiResponse.Failure) { - Firebase.auth.signOut() - } - return withContext(coroutineDispatcher) { signInWithMoyeoRun(idToken) } - } - - private suspend fun signInWithFirebaseCredential(idToken: String): ApiResponse { - val firebaseAuth = Firebase.auth - return suspendCancellableCoroutine { - val firebaseCredential = GoogleAuthProvider.getCredential(idToken, null) - firebaseAuth.signInWithCredential(firebaseCredential) - .addOnCompleteListener { task -> - val firebaseUser = firebaseAuth.currentUser - if (firebaseUser != null) { - it.resume(ApiResponse.Success(firebaseUser)) - } else { - it.resume( - ApiResponse.Failure( - task.exception - ?: IllegalStateException("FirebaseAuth Failure: FirebaseUser and Task.exception is null") - ) - ) - } - } - } - } - - private suspend fun signInWithMoyeoRun(idToken: String): ApiResponse { - //Todo: 모여런 서버 signIn - return ApiResponse.Success(Unit) - } - - // Todo: 임시. 네트워크 베이스 코드가 Rebase 되면 대체할 것 - sealed class ApiResponse { - data class Success(val data: T) : ApiResponse() - data class Failure(val error: Throwable) : ApiResponse() - } -} \ No newline at end of file diff --git a/app/src/main/java/com/moyerun/moyeorun_android/login/data/LoginRepository.kt b/app/src/main/java/com/moyerun/moyeorun_android/login/data/LoginRepository.kt new file mode 100644 index 0000000..f274569 --- /dev/null +++ b/app/src/main/java/com/moyerun/moyeorun_android/login/data/LoginRepository.kt @@ -0,0 +1,18 @@ +package com.moyerun.moyeorun_android.login.data + +import kotlinx.coroutines.CoroutineDispatcher +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.withContext + +class LoginRepository( + private val coroutineDispatcher: CoroutineDispatcher = Dispatchers.IO +) { + + // Todo: 모여런 서버 붙일 때 반환 타입 조정 + suspend fun signIn(idToken: String): LoginResponse { + return withContext(coroutineDispatcher) { + // Todo: 모여런 서버 signIn + LoginResponse(TokenPair(Token("", ""), Token("", "")), true, "1234") + } + } +} \ No newline at end of file diff --git a/app/src/main/java/com/moyerun/moyeorun_android/login/data/LoginResponse.kt b/app/src/main/java/com/moyerun/moyeorun_android/login/data/LoginResponse.kt new file mode 100644 index 0000000..5bf006a --- /dev/null +++ b/app/src/main/java/com/moyerun/moyeorun_android/login/data/LoginResponse.kt @@ -0,0 +1,12 @@ +package com.moyerun.moyeorun_android.login.data + +data class LoginResponse( + val token: TokenPair, + val isNewUser: Boolean, + val userId: String +) + +data class TokenPair( + val accessToken: Token, + val refreshToken: Token +) \ No newline at end of file diff --git a/app/src/main/java/com/moyerun/moyeorun_android/login/data/Token.kt b/app/src/main/java/com/moyerun/moyeorun_android/login/data/Token.kt new file mode 100644 index 0000000..2de410a --- /dev/null +++ b/app/src/main/java/com/moyerun/moyeorun_android/login/data/Token.kt @@ -0,0 +1,6 @@ +package com.moyerun.moyeorun_android.login.data + +data class Token( + val token: String, + val expiresIn: String +) diff --git a/app/src/main/java/com/moyerun/moyeorun_android/login/LoginActivity.kt b/app/src/main/java/com/moyerun/moyeorun_android/login/ui/LoginActivity.kt similarity index 98% rename from app/src/main/java/com/moyerun/moyeorun_android/login/LoginActivity.kt rename to app/src/main/java/com/moyerun/moyeorun_android/login/ui/LoginActivity.kt index ebe0ad6..a21f8d2 100644 --- a/app/src/main/java/com/moyerun/moyeorun_android/login/LoginActivity.kt +++ b/app/src/main/java/com/moyerun/moyeorun_android/login/ui/LoginActivity.kt @@ -1,4 +1,4 @@ -package com.moyerun.moyeorun_android.login +package com.moyerun.moyeorun_android.login.ui import android.content.Intent import android.content.IntentSender @@ -69,7 +69,7 @@ class LoginActivity : AppCompatActivity() { observeEvent(viewModel.loginEvent) { when (it) { - LoginEvent.Success -> { + LoginEvent.RegisteredUser -> { // Todo: 메인화면 진입 Lg.d("Login!") } diff --git a/app/src/main/java/com/moyerun/moyeorun_android/login/ui/LoginEvent.kt b/app/src/main/java/com/moyerun/moyeorun_android/login/ui/LoginEvent.kt new file mode 100644 index 0000000..f406b21 --- /dev/null +++ b/app/src/main/java/com/moyerun/moyeorun_android/login/ui/LoginEvent.kt @@ -0,0 +1,5 @@ +package com.moyerun.moyeorun_android.login.ui + +enum class LoginEvent { + RegisteredUser, NewUser, Error +} \ No newline at end of file diff --git a/app/src/main/java/com/moyerun/moyeorun_android/login/LoginViewModel.kt b/app/src/main/java/com/moyerun/moyeorun_android/login/ui/LoginViewModel.kt similarity index 70% rename from app/src/main/java/com/moyerun/moyeorun_android/login/LoginViewModel.kt rename to app/src/main/java/com/moyerun/moyeorun_android/login/ui/LoginViewModel.kt index 8c97903..824e7fe 100644 --- a/app/src/main/java/com/moyerun/moyeorun_android/login/LoginViewModel.kt +++ b/app/src/main/java/com/moyerun/moyeorun_android/login/ui/LoginViewModel.kt @@ -1,9 +1,10 @@ -package com.moyerun.moyeorun_android.login +package com.moyerun.moyeorun_android.login.ui import androidx.lifecycle.ViewModel import androidx.lifecycle.viewModelScope import com.moyerun.moyeorun_android.common.EventLiveData import com.moyerun.moyeorun_android.common.MutableEventLiveData +import com.moyerun.moyeorun_android.login.data.LoginRepository import dagger.hilt.android.lifecycle.HiltViewModel import kotlinx.coroutines.flow.MutableStateFlow import kotlinx.coroutines.flow.StateFlow @@ -26,12 +27,14 @@ class LoginViewModel @Inject constructor( fun signIn(idToken: String) { viewModelScope.launch { _isLoading.value = true + // Todo: 반환 타입 결정 후 분기 + // Todo: Firebase crashlytics userId 세팅 + // Todo: SharedPreference 에 유저 메타데이터 세팅 val result = loginRepository.signIn(idToken) - if (result is LoginRepository.ApiResponse.Success) { - // Todo: Firebase crashlytics userId 세팅 - _loginEvent.event = LoginEvent.Success + _loginEvent.event = if (result.isNewUser) { + LoginEvent.NewUser } else { - _loginEvent.event = LoginEvent.Error + LoginEvent.RegisteredUser } _isLoading.value = false } diff --git a/app/src/main/res/layout/activity_login.xml b/app/src/main/res/layout/activity_login.xml index 615a8aa..fd71795 100644 --- a/app/src/main/res/layout/activity_login.xml +++ b/app/src/main/res/layout/activity_login.xml @@ -5,7 +5,7 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context=".login.LoginActivity"> + tools:context=".login.ui.LoginActivity">