Skip to content

Commit

Permalink
[FEAT/#68] 카카오 로그인 토큰 API 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
Marchbreeze committed Aug 13, 2024
1 parent 260b130 commit 84d64c8
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import co.orange.core.base.BaseActivity
import co.orange.core.extension.setOnSingleClickListener
import co.orange.core.extension.stringOf
import co.orange.core.extension.toast
import co.orange.core.state.UiState
import co.orange.presentation.auth.phone.PhoneActivity
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.flow.distinctUntilChanged
Expand All @@ -27,7 +26,7 @@ class LoginActivity : BaseActivity<ActivityLoginBinding>(R.layout.activity_login

initLoginBtnListener()
observeAppLoginAvailable()
observeChangeTokenState()
observeChangeTokenResult()
}

private fun initLoginBtnListener() {
Expand All @@ -42,19 +41,16 @@ class LoginActivity : BaseActivity<ActivityLoginBinding>(R.layout.activity_login
}.launchIn(lifecycleScope)
}

private fun observeChangeTokenState() {
viewModel.changeTokenState.flowWithLifecycle(lifecycle).distinctUntilChanged()
.onEach { state ->
when (state) {
is UiState.Success -> {
Intent(this, PhoneActivity::class.java).apply {
startActivity(this)
}
finish()
private fun observeChangeTokenResult() {
viewModel.changeTokenResult.flowWithLifecycle(lifecycle).distinctUntilChanged()
.onEach { isSuccess ->
if (isSuccess) {
Intent(this, PhoneActivity::class.java).apply {
startActivity(this)
}

is UiState.Failure -> toast(stringOf(R.string.error_msg))
else -> return@onEach
finish()
} else {
toast(stringOf(R.string.error_msg))
}
}.launchIn(lifecycleScope)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,17 @@ package co.orange.presentation.auth.login
import android.content.Context
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import co.orange.core.state.UiState
import co.orange.domain.entity.request.AuthRequestModel
import co.orange.domain.repository.AuthRepository
import co.orange.domain.repository.UserRepository
import com.kakao.sdk.auth.model.OAuthToken
import com.kakao.sdk.common.model.ClientError
import com.kakao.sdk.common.model.ClientErrorCause
import com.kakao.sdk.user.UserApiClient
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.launch
import javax.inject.Inject
Expand All @@ -26,8 +28,8 @@ class LoginViewModel
private val _isAppLoginAvailable = MutableStateFlow(true)
val isAppLoginAvailable: StateFlow<Boolean> = _isAppLoginAvailable

private val _changeTokenState = MutableStateFlow<UiState<String>>(UiState.Empty)
val changeTokenState: StateFlow<UiState<String>> = _changeTokenState
private val _changeTokenResult = MutableSharedFlow<Boolean>()
val changeTokenResult: SharedFlow<Boolean> = _changeTokenResult

private var appLoginCallback: (OAuthToken?, Throwable?) -> Unit = { token, error ->
if (error != null) {
Expand Down Expand Up @@ -64,13 +66,12 @@ class LoginViewModel
authRepository.postOauthDataToGetToken(AuthRequestModel(accessToken, KAKAO))
.onSuccess {
with(userRepository) {
setTokens(it.accessToken, it.refreshToken)
setUserRole(it.userRoleString)
setTokens(it.accesstoken, it.refreshtoken)
}
_changeTokenState.value = UiState.Success(it.userRoleString)
_changeTokenResult.emit(true)
}
.onFailure {
_changeTokenState.value = UiState.Failure(it.message.toString())
_changeTokenResult.emit(false)
}
}
}
Expand Down

0 comments on commit 84d64c8

Please sign in to comment.