Skip to content

Commit

Permalink
Merge pull request #106 from Team-Going/hotfix/#105-auth-dto
Browse files Browse the repository at this point in the history
[Hotfix/#105] auth dto 핫픽스
  • Loading branch information
chattymin authored Jan 14, 2024
2 parents 7d2a070 + 2738cac commit 8056f3f
Show file tree
Hide file tree
Showing 23 changed files with 97 additions and 69 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,18 +64,12 @@ class AuthInterceptor @Inject constructor(
return chain.proceed(newRequest)
}

dataStore.apply {
accessToken = ""
refreshToken = ""
}
dataStore.clearInfo()

// refreshToken 만료 처리를 위한 리프레시 토큰 만료 코드 포함 리스폰스 리턴
return refreshTokenResponse
} catch (t: Throwable) {
dataStore.apply {
accessToken = ""
refreshToken = ""
}
dataStore.clearInfo()

Timber.e(t)
}
Expand All @@ -84,8 +78,8 @@ class AuthInterceptor @Inject constructor(
return response
}

private fun Request.newAuthBuilder() =
this.newBuilder().addHeader(AUTHORIZATION, "$BEARER ${dataStore.accessToken}")
private fun Request.newAuthBuilder() =
this.newBuilder().addHeader(AUTHORIZATION, "$BEARER ${dataStore.accessToken}")

companion object {
private const val CODE_TOKEN_EXPIRED = 401
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ data class AuthResponseDto(
val accessToken: String,
@SerialName("refreshToken")
val refreshToken: String,
@SerialName("userId")
val userId: Long,
) {
fun toAuthTokenModel() =
AuthTokenModel(accessToken = accessToken, refreshToken = refreshToken)
AuthTokenModel(accessToken = accessToken, refreshToken = refreshToken, userId = userId)
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ data class SignInResponseDto(
val refreshToken: String,
@SerialName("isResult")
val isResult: Boolean,
@SerialName("userId")
val userId: Long,
) {
fun toSignInModel() =
SignInModel(accessToken, refreshToken, isResult)
SignInModel(accessToken, refreshToken, isResult, userId)
}
2 changes: 2 additions & 0 deletions data/src/main/java/com/going/data/local/GoingDataStore.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ package com.going.data.local
interface GoingDataStore {
var accessToken: String
var refreshToken: String
var userId: Long
fun clearInfo()
}
9 changes: 9 additions & 0 deletions data/src/main/java/com/going/data/local/GoingDataStoreImpl.kt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,17 @@ class GoingDataStoreImpl @Inject constructor(
get() = dataStore.getString(REFRESH_TOKEN, "") ?: ""
set(value) = dataStore.edit { putString(REFRESH_TOKEN, value) }

override var userId: Long
get() = dataStore.getLong(USER_ID, 0L)
set(value) = dataStore.edit { putLong(USER_ID, value) }

override fun clearInfo() {
dataStore.edit().clear().commit()
}

companion object {
private const val ACCESS_TOKEN = "ACCESS_TOKEN"
private const val REFRESH_TOKEN = "REFRESH_TOKEN"
private const val USER_ID = "USER_ID"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,11 @@ class TokenRepositoryImpl @Inject constructor(
goingDataStore.refreshToken = refreshToken
}

override fun clearTokens() {
goingDataStore.accessToken = ""
goingDataStore.refreshToken = ""
override fun setUserId(userId: Long) {
goingDataStore.userId = userId
}

override fun clearInfo() {
goingDataStore.clearInfo()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ package com.going.domain.entity.response
data class AuthTokenModel(
val accessToken: String,
val refreshToken: String,
val userId: Long,
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ data class SignInModel(
val accessToken: String,
val refreshToken: String,
val isResult: Boolean,
val userId: Long,
)
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ interface TokenRepository {

fun setTokens(accessToken: String, refreshToken: String)

fun clearTokens()
fun setUserId(userId: Long)

fun clearInfo()
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.os.Bundle
import androidx.activity.viewModels
import com.going.presentation.R
import com.going.presentation.databinding.ActivityTripDashBoardBinding
import com.going.presentation.setting.SettingActivity
import com.going.presentation.entertrip.starttrip.invitetrip.CreateTripActivity
import com.going.ui.base.BaseActivity
import com.going.ui.extension.setOnSingleClickListener
Expand All @@ -25,8 +26,8 @@ class DashBoardActivity :
setTabLayout()
setViewPager()
setTravelerName()
initSettingBtnClickListener()
initCreateTripBtnClickListener()

}

private fun setTabLayout() {
Expand Down Expand Up @@ -55,6 +56,18 @@ class DashBoardActivity :
}
}

private fun initSettingBtnClickListener() {
binding.btnDashboardSetting.setOnSingleClickListener {
navigateToSettingScreen()
}
}

private fun navigateToSettingScreen() {
Intent(this, SettingActivity::class.java).apply {
startActivity(this)
}
}

private fun initCreateTripBtnClickListener() {
binding.btnDashboardCreateTrip.setOnSingleClickListener {
navigateToDashboard()
Expand All @@ -63,6 +76,7 @@ class DashBoardActivity :

private fun navigateToDashboard() {
Intent(this, CreateTripActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(this)
}
}
Expand All @@ -71,5 +85,4 @@ class DashBoardActivity :
const val TAB_ONGOING = "진행중인 여행"
const val TAB_COMPLETED = "지나간 여행"
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import androidx.lifecycle.lifecycleScope
import com.going.domain.entity.CodeState
import com.going.presentation.R
import com.going.presentation.databinding.ActivityEnterTripBinding
import com.going.presentation.entertrip.starttrip.StartTripSplashActivity
import com.going.presentation.entertrip.starttrip.invitetrip.InviteFinishActivity
import com.going.ui.base.BaseActivity
import com.going.ui.extension.UiState
Expand All @@ -32,15 +31,10 @@ class EnterTripActivity : BaseActivity<ActivityEnterTripBinding>(R.layout.activi
observeIsCodeAvailable()
initNextBtnClickListener()
observeEnterTripState()


}

private fun initBackBtnClickListener() {
binding.btnEnterBack.setOnSingleClickListener {
Intent(this, StartTripSplashActivity::class.java).apply {
startActivity(this)
}
finish()
}
}
Expand Down Expand Up @@ -85,12 +79,10 @@ class EnterTripActivity : BaseActivity<ActivityEnterTripBinding>(R.layout.activi
counter.setTextColor(getColor(color))
}


private fun observeEnterTripState() {
viewModel.tripState.flowWithLifecycle(lifecycle).onEach { state ->
when (state) {
is UiState.Success -> {

Intent(this, InviteFinishActivity::class.java).apply {
putExtra(TRIP_ID, state.data.tripId)
putExtra(TITLE, state.data.title)
Expand Down Expand Up @@ -126,5 +118,4 @@ class EnterTripActivity : BaseActivity<ActivityEnterTripBinding>(R.layout.activi
const val CODE = "code"
const val DAY = "day"
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import androidx.core.content.res.ResourcesCompat
import com.going.domain.entity.NameState
import com.going.presentation.R
import com.going.presentation.databinding.ActivityCreateTripBinding
import com.going.presentation.entertrip.starttrip.StartTripSplashActivity
import com.going.presentation.entertrip.starttrip.createtrip.BottomSheetDateContentFragment
import com.going.presentation.entertrip.starttrip.createtrip.EnterPreferenceActivity
import com.going.ui.base.BaseActivity
Expand Down Expand Up @@ -37,9 +36,7 @@ class CreateTripActivity :

private fun initBackBtnClickListener() {
binding.tbCreateTrip.setOnSingleClickListener {
Intent(this, StartTripSplashActivity::class.java).apply {
startActivity(this)
}
finish()
}
}

Expand Down Expand Up @@ -76,14 +73,12 @@ class CreateTripActivity :
}
}


private fun observeCheckStartDateAvailable() {
viewModel.isStartDateAvailable.observe(this) { isAvailable ->
if (isAvailable) {
setStartDateColors(
binding.tvCreateTripStartDate
)
{ background ->
binding.tvCreateTripStartDate,
) { background ->
binding.tvCreateTripStartDate.background = ResourcesCompat.getDrawable(
this.resources,
background,
Expand All @@ -98,9 +93,8 @@ class CreateTripActivity :
viewModel.isEndDateAvailable.observe(this) { isAvailable ->
if (isAvailable) {
setEndDateColors(
binding.tvCreateTripEndDate
)
{ background ->
binding.tvCreateTripEndDate,
) { background ->
binding.tvCreateTripEndDate.background = ResourcesCompat.getDrawable(
this.resources,
background,
Expand Down Expand Up @@ -159,7 +153,6 @@ class CreateTripActivity :
counter.setTextColor(getColor(color))
}


private fun initStartDateClickListener() {
binding.tvCreateTripStartDate.setOnSingleClickListener {
startBottomSheetDialog = BottomSheetDateContentFragment(viewModel, true)
Expand Down Expand Up @@ -199,6 +192,3 @@ class CreateTripActivity :
const val END_DAY = "endDay"
}
}



Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class SignInViewModel @Inject constructor(
viewModelScope.launch {
authRepository.postSignIn(accessToken, SignInRequestModel(platform)).onSuccess {
tokenRepository.setTokens(it.accessToken, it.refreshToken)
tokenRepository.setUserId(it.userId)

if (it.isResult) {
_postChangeTokenState.value = AuthState.SUCCESS
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import com.going.domain.entity.NameState
import com.going.presentation.R
import com.going.presentation.databinding.ActivityOnboardingProfileSettingBinding
import com.going.presentation.onboarding.splash.SplashActivity
import com.going.presentation.tendency.splash.TendencySplashActivity
import com.going.presentation.tendency.ttest.TendencyTestActivity
import com.going.ui.base.BaseActivity
import com.going.ui.extension.setOnSingleClickListener
Expand Down Expand Up @@ -152,7 +153,7 @@ class OnboardingProfileSettingActivity :
viewModel.isSignUpState.flowWithLifecycle(lifecycle).onEach { state ->
when (state) {
AuthState.LOADING -> return@onEach
AuthState.SUCCESS -> navigateToTendencyTestScreen()
AuthState.SUCCESS -> navigateToTendencySplashScreen()
AuthState.FAILURE -> toast(getString(R.string.server_error))
AuthState.SIGNUP -> return@onEach
AuthState.SIGNIN -> navigateToSplashScreen()
Expand All @@ -170,8 +171,8 @@ class OnboardingProfileSettingActivity :
finish()
}

private fun navigateToTendencyTestScreen() {
Intent(this, TendencyTestActivity::class.java).apply {
private fun navigateToTendencySplashScreen() {
Intent(this, TendencySplashActivity::class.java).apply {
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
startActivity(this)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.going.domain.entity.AuthState
import com.going.domain.entity.NameState
import com.going.domain.entity.request.SignUpRequestModel
import com.going.domain.repository.AuthRepository
import com.going.domain.repository.TokenRepository
import com.kakao.sdk.auth.AuthApiClient
import com.kakao.sdk.auth.TokenManagerProvider
import com.kakao.sdk.user.UserApiClient
Expand All @@ -20,6 +21,7 @@ import javax.inject.Inject
@HiltViewModel
class OnboardingProfileSettingViewModel @Inject constructor(
private val authRepository: AuthRepository,
private val tokenRepository: TokenRepository,
) : ViewModel() {

val name = MutableStateFlow("")
Expand Down Expand Up @@ -88,6 +90,8 @@ class OnboardingProfileSettingViewModel @Inject constructor(
kakaoAccessToken,
SignUpRequestModel(name.value, info.value, KAKAO),
).onSuccess {
tokenRepository.setTokens(it.accessToken, it.refreshToken)
tokenRepository.setUserId(it.userId)
_isSignUpState.value = AuthState.SUCCESS
}.onFailure {
_isSignUpState.value = AuthState.FAILURE
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,23 @@ class SplashViewModel @Inject constructor(
private val _userState = MutableStateFlow(AuthState.LOADING)
val userState: StateFlow<AuthState> = _userState
fun getHasAccessToken(): Boolean = tokenRepository.getAccessToken().isNotBlank()
fun clear() = tokenRepository.clearTokens()

fun getUserState() {
viewModelScope.launch {
authRepository.getSplash().onSuccess {
_userState.value = AuthState.SUCCESS
}.onFailure {
val errorCode = toErrorCode(it)
if (tokenRepository.getAccessToken() != "") {
viewModelScope.launch {
authRepository.getSplash().onSuccess {
_userState.value = AuthState.SUCCESS
}.onFailure {
val errorCode = toErrorCode(it)

_userState.value = when (errorCode) {
TENDENCY -> AuthState.TENDENCY
else -> AuthState.FAILURE
_userState.value = when (errorCode) {
TENDENCY -> AuthState.TENDENCY
else -> AuthState.FAILURE
}
}
}
} else {
_userState.value = AuthState.FAILURE
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class SettingActivity : BaseActivity<ActivitySettingBinding>(R.layout.activity_s
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

initBackBtnClickListener()
initProfileClickListener()
initInquireClickListener()
setVersionCode()
Expand All @@ -28,6 +29,12 @@ class SettingActivity : BaseActivity<ActivitySettingBinding>(R.layout.activity_s
initQuitClickListener()
}

private fun initBackBtnClickListener() {
binding.btnPreferenceBack.setOnSingleClickListener {
finish()
}
}

private fun initProfileClickListener() {
binding.btnSettingProfile.setOnSingleClickListener {
}
Expand Down
Loading

0 comments on commit 8056f3f

Please sign in to comment.