Skip to content

Commit

Permalink
Merge pull request #35 from AdultOfNineteen/feature/issue-34
Browse files Browse the repository at this point in the history
[BUG] 스플래시 / 로그인 로직 수정
  • Loading branch information
DongChyeon authored Jan 22, 2024
2 parents 7498887 + a934eb9 commit efd848f
Show file tree
Hide file tree
Showing 13 changed files with 118 additions and 77 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.teamwiney.core.common.model

enum class UserStatus {
ACTIVE, INACTIVE
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.teamwiney.data.network.model.response.DeleteUser
import com.teamwiney.data.network.model.response.GoogleAccessToken
import com.teamwiney.data.network.model.response.SetPreferences
import com.teamwiney.data.network.model.response.SocialLogin
import com.teamwiney.data.network.model.response.UserInfo
import com.teamwiney.data.network.model.response.VerifyAuthenticationMessage
import kotlinx.coroutines.flow.Flow

Expand Down Expand Up @@ -47,6 +48,8 @@ interface AuthDataSource {

fun refreshToken(refreshToken: String): Flow<ApiResult<ResponseWrapper<AccessToken>>>

fun getUserInfo(): Flow<ApiResult<ResponseWrapper<UserInfo>>>

fun getConnections(): Flow<ApiResult<BaseResponse>>

fun registerFcmToken(fcmTokenRequest: FcmTokenRequest): Flow<ApiResult<BaseResponse>>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ class AuthDataSourceImpl @Inject constructor(
emit(authService.refreshToken(refreshToken))
}.flowOn(ioDispatcher)

override fun getUserInfo() = flow {
emit(authService.getUserInfo())
}.flowOn(ioDispatcher)

override fun getConnections() = flow {
emit(authService.getConnections())
}.flowOn(ioDispatcher)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package com.teamwiney.data.network.model.response

import com.teamwiney.core.common.model.UserStatus

/**
* - userStatus
1. 취향설정 까지 모두 마쳐 회원 가입이 완료된 경우 → `ACTIVE`
Expand All @@ -21,12 +23,7 @@ data class SocialLogin(
val accessToken: String,
val userId: Int,
val refreshToken: String,
val userStatus: String,
val userStatus: UserStatus,
val messageStatus: String,
val preferenceStatus: String
) {
companion object {
const val USER_STATUS_ACTIVE = "ACTIVE"
const val USER_STATUS_INACTIVE = "INACTIVE"
}
}
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.teamwiney.data.network.model.response

import com.google.gson.annotations.SerializedName
import com.teamwiney.core.common.model.UserStatus

data class UserInfo(
@SerializedName("userId") val userId: Int,
@SerializedName("status") val status: UserStatus
)
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import com.teamwiney.data.network.model.response.DeleteUser
import com.teamwiney.data.network.model.response.GoogleAccessToken
import com.teamwiney.data.network.model.response.SetPreferences
import com.teamwiney.data.network.model.response.SocialLogin
import com.teamwiney.data.network.model.response.UserInfo
import com.teamwiney.data.network.model.response.VerifyAuthenticationMessage
import retrofit2.Response
import retrofit2.http.Body
Expand Down Expand Up @@ -69,6 +70,9 @@ interface AuthService {
@Body preferences: SetPreferencesRequest
): ApiResult<ResponseWrapper<SetPreferences>>

/** 유저 상태 정보 조회 API */
@GET("/info")
suspend fun getUserInfo(): ApiResult<ResponseWrapper<UserInfo>>

/** 토큰 리프레쉬 API */
@POST("/refresh")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.teamwiney.data.network.model.response.DeleteUser
import com.teamwiney.data.network.model.response.GoogleAccessToken
import com.teamwiney.data.network.model.response.SetPreferences
import com.teamwiney.data.network.model.response.SocialLogin
import com.teamwiney.data.network.model.response.UserInfo
import com.teamwiney.data.network.model.response.VerifyAuthenticationMessage
import kotlinx.coroutines.flow.Flow

Expand Down Expand Up @@ -49,6 +50,8 @@ interface AuthRepository {
refreshToken: String
): Flow<ApiResult<ResponseWrapper<AccessToken>>>

fun getUserInfo(): Flow<ApiResult<ResponseWrapper<UserInfo>>>

fun getConnections(): Flow<ApiResult<BaseResponse>>

fun registerFcmToken(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ class AuthRepositoryImpl @Inject constructor(
request: PhoneNumberWithVerificationCodeRequest
) = authDataSource.verifyAuthCodeMessage(userId, request)

override fun getUserInfo() = authDataSource.getUserInfo()

override fun getConnections() = authDataSource.getConnections()

override fun registerFcmToken(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ import com.kakao.sdk.common.model.ClientErrorCause
import com.kakao.sdk.user.UserApiClient
import com.teamwiney.core.common.base.BaseViewModel
import com.teamwiney.core.common.model.SocialType
import com.teamwiney.core.common.model.UserStatus
import com.teamwiney.core.common.navigation.AuthDestinations
import com.teamwiney.core.common.navigation.HomeDestinations
import com.teamwiney.core.common.util.Constants
import com.teamwiney.core.common.util.Constants.ACCESS_TOKEN
import com.teamwiney.core.common.util.Constants.LOGIN_TYPE
import com.teamwiney.core.common.util.Constants.REFRESH_TOKEN
import com.teamwiney.data.network.adapter.ApiResult
import com.teamwiney.data.network.model.response.SocialLogin
import com.teamwiney.data.repository.auth.AuthRepository
import com.teamwiney.data.repository.persistence.DataStoreRepository
import dagger.hilt.android.lifecycle.HiltViewModel
Expand Down Expand Up @@ -111,26 +111,27 @@ class LoginViewModel @Inject constructor(
updateState(currentState.copy(isLoading = false))
when (result) {
is ApiResult.Success -> {
runBlocking {
dataStoreRepository.setStringValue(ACCESS_TOKEN, result.data.result.accessToken)
dataStoreRepository.setStringValue(REFRESH_TOKEN, result.data.result.refreshToken)
dataStoreRepository.setIntValue(Constants.USER_ID, result.data.result.userId)
}

Log.i(
"[ACCESS_TOKEN]",
"accessToken: ${result.data.result.accessToken}"
)
Log.i(
"[REFRESH_TOKEN]",
"refreshToken: ${result.data.result.refreshToken}"
)

val userStatus = result.data.result.userStatus
if (userStatus == SocialLogin.USER_STATUS_ACTIVE) {
Log.i(
"[ACCESS_TOKEN]",
"accessToken: ${result.data.result.accessToken}"
)
Log.i(
"[REFRESH_TOKEN]",
"refreshToken: ${result.data.result.refreshToken}"
)
dataStoreRepository.setStringValue(
ACCESS_TOKEN,
result.data.result.accessToken
)
dataStoreRepository.setStringValue(
REFRESH_TOKEN,
result.data.result.refreshToken
)
if (userStatus == UserStatus.ACTIVE) {
dataStoreRepository.setStringValue(LOGIN_TYPE, socialType.name)

registerFcmToken()

postEffect(LoginContract.Effect.NavigateTo(
destination = HomeDestinations.ROUTE,
navOptions = navOptions {
Expand All @@ -142,10 +143,6 @@ class LoginViewModel @Inject constructor(
} else {
postEffect(LoginContract.Effect.NavigateTo("${AuthDestinations.SignUp.ROUTE}?userId=${result.data.result.userId}"))
}

runBlocking {
dataStoreRepository.setIntValue(Constants.USER_ID, result.data.result.userId)
}
}

is ApiResult.ApiError -> {
Expand All @@ -160,4 +157,23 @@ class LoginViewModel @Inject constructor(
}
}

private fun registerFcmToken() = viewModelScope.launch {
val fcmToken = dataStoreRepository.getStringValue(Constants.FCM_TOKEN).first()
val deviceId = dataStoreRepository.getStringValue(Constants.DEVICE_ID).first()

authRepository.registerFcmToken(fcmToken, deviceId).collectLatest {
when (it) {
is ApiResult.ApiError -> {
postEffect(LoginContract.Effect.ShowSnackBar(it.message))
}

is ApiResult.NetworkError -> {
postEffect(LoginContract.Effect.ShowSnackBar("네트워크 오류가 발생했습니다."))
}

else -> { }
}
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,24 @@ import androidx.lifecycle.viewModelScope
import com.teamwiney.auth.signup.component.state.SignUpFavoriteCategoryUiState
import com.teamwiney.core.common.base.BaseViewModel
import com.teamwiney.core.common.navigation.AuthDestinations
import com.teamwiney.core.common.util.Constants
import com.teamwiney.data.network.adapter.ApiResult
import com.teamwiney.data.network.model.request.PhoneNumberRequest
import com.teamwiney.data.network.model.request.PhoneNumberWithVerificationCodeRequest
import com.teamwiney.data.network.model.request.SetPreferencesRequest
import com.teamwiney.data.repository.auth.AuthRepository
import com.teamwiney.data.repository.persistence.DataStoreRepository
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.first
import kotlinx.coroutines.flow.onStart
import kotlinx.coroutines.launch
import javax.inject.Inject

@HiltViewModel
class SignUpViewModel @Inject constructor(
private val authRepository: AuthRepository
private val authRepository: AuthRepository,
private val dataStoreRepository: DataStoreRepository
) : BaseViewModel<SignUpContract.State, SignUpContract.Event, SignUpContract.Effect>(
initialState = SignUpContract.State()
) {
Expand Down Expand Up @@ -57,6 +61,7 @@ class SignUpViewModel @Inject constructor(
).collectLatest {
when (it) {
is ApiResult.Success -> {
registerFcmToken() // 회원가입 완료 후 FCM 토큰 등록
postEffect(SignUpContract.Effect.NavigateTo(AuthDestinations.SignUp.COMPLETE))
}

Expand Down Expand Up @@ -132,6 +137,25 @@ class SignUpViewModel @Inject constructor(
}
}

private fun registerFcmToken() = viewModelScope.launch {
val fcmToken = dataStoreRepository.getStringValue(Constants.FCM_TOKEN).first()
val deviceId = dataStoreRepository.getStringValue(Constants.DEVICE_ID).first()

authRepository.registerFcmToken(fcmToken, deviceId).collectLatest {
when (it) {
is ApiResult.ApiError -> {
postEffect(SignUpContract.Effect.ShowSnackBar(it.message))
}

is ApiResult.NetworkError -> {
postEffect(SignUpContract.Effect.ShowSnackBar("네트워크 오류가 발생했습니다."))
}

else -> { }
}
}
}

fun updateUserId(userId: String) = viewModelScope.launch {
updateState(currentState.copy(userId = userId))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class SplashContract {
) : UiState

sealed class Event : UiEvent {
object AutoLoginCheck : Event()
object CheckUserStatus : Event()
}

sealed class Effect : UiEffect {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,8 @@ fun SplashScreen(
LaunchedEffect(true) {
viewModel.checkIsFirstLaunch()

viewModel.getConnections()
viewModel.registerFcmToken()
delay(1500)
viewModel.processEvent(SplashContract.Event.AutoLoginCheck)
viewModel.processEvent(SplashContract.Event.CheckUserStatus)

effectFlow.collectLatest { effect ->
when (effect) {
Expand Down
Loading

0 comments on commit efd848f

Please sign in to comment.