Skip to content

Commit

Permalink
[FEAT]#30: 자동로그인 로직 수정해요
Browse files Browse the repository at this point in the history
  • Loading branch information
flash159483 committed Jul 19, 2024
1 parent 8fdbcf6 commit 952078b
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 39 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ data class SignUp(
val grade: Int,
val classNumber: Int,
val gender: String,
val consents: Consents
val consents: Consents,
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ package com.bff.wespot.model.auth.response

data class Consents(
val marketing: Boolean,
)
)
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.bff.wespot.model.auth.response


data class SignUpToken(
val signUpToken: String,
)
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package com.bff.wespot.domain.usecase

import com.bff.wespot.domain.repository.DataStoreRepository
import com.bff.wespot.domain.util.DataStoreKey
import com.bff.wespot.model.constants.LoginState
import kotlinx.coroutines.flow.first
import java.time.LocalDateTime
import java.time.format.DateTimeFormatter
import javax.inject.Inject

class AutoLoginUseCase @Inject constructor(
private val dataStoreRepository: DataStoreRepository,
) {
suspend operator fun invoke(): LoginState =
dataStoreRepository.getString(DataStoreKey.REFRESH_TOKEN_EXPIRED_AT).first().let {
val dateTime = LocalDateTime.parse(it, DateTimeFormatter.ISO_DATE_TIME)
val now = LocalDateTime.now()

return if (it.isEmpty() || dateTime.isBefore(now)) {
LoginState.LOGIN_FAILURE
} else {
LoginState.LOGIN_SUCCESS
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class KakaoLoginUseCase @Inject constructor(
dataStoreRepository.saveString(DataStoreKey.REFRESH_TOKEN, it.refreshToken)
dataStoreRepository.saveString(
DataStoreKey.REFRESH_TOKEN_EXPIRED_AT,
it.refreshTokenExpiredAt
it.refreshTokenExpiredAt,
)
LoginState.LOGIN_SUCCESS
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -343,14 +343,18 @@ private fun RegisterBottomSheetContent(
url = "",
checked = checked[1],
onClicked = {
action(AuthAction.OnConsentChanged(checked.toMutableList().apply {
if (this[1]) {
this[1] = false
this[0] = false
} else {
this[1] = true
}
}))
action(
AuthAction.OnConsentChanged(
checked.toMutableList().apply {
if (this[1]) {
this[1] = false
this[0] = false
} else {
this[1] = true
}
},
),
)
},
)

Expand All @@ -359,14 +363,18 @@ private fun RegisterBottomSheetContent(
url = "",
checked = checked[2],
onClicked = {
action(AuthAction.OnConsentChanged(checked.toMutableList().apply {
if (this[2]) {
this[2] = false
this[0] = false
} else {
this[2] = true
}
}))
action(
AuthAction.OnConsentChanged(
checked.toMutableList().apply {
if (this[2]) {
this[2] = false
this[0] = false
} else {
this[2] = true
}
},
),
)
},
)

Expand All @@ -375,14 +383,18 @@ private fun RegisterBottomSheetContent(
url = "",
checked = checked[3],
onClicked = {
action(AuthAction.OnConsentChanged(checked.toMutableList().apply {
if (this[3]) {
this[3] = false
this[0] = false
} else {
this[3] = true
}
}))
action(
AuthAction.OnConsentChanged(
checked.toMutableList().apply {
if (this[3]) {
this[3] = false
this[0] = false
} else {
this[3] = true
}
},
),
)
},
)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ data class AuthUiState(
val gender: String = "",
val name: String = "",
val loading: Boolean = false,
val consents: List<Boolean> = listOf(false, false, false, false)
val consents: List<Boolean> = listOf(false, false, false, false),
)
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ import com.bff.wespot.auth.state.AuthUiState
import com.bff.wespot.auth.state.NavigationAction
import com.bff.wespot.domain.repository.DataStoreRepository
import com.bff.wespot.domain.repository.auth.AuthRepository
import com.bff.wespot.domain.usecase.AutoLoginUseCase
import com.bff.wespot.domain.usecase.KakaoLoginUseCase
import com.bff.wespot.domain.util.DataStoreKey
import com.bff.wespot.model.auth.request.SignUp
import com.bff.wespot.model.auth.response.Consents
import com.bff.wespot.model.auth.response.School
Expand All @@ -36,6 +36,7 @@ class AuthViewModel @Inject constructor(
private val authRepository: AuthRepository,
private val dispatcher: CoroutineDispatcher,
private val dataStoreRepository: DataStoreRepository,
private val autoLoginUseCase: AutoLoginUseCase,
) : ViewModel(), ContainerHost<AuthUiState, AuthSideEffect> {
override val container = container<AuthUiState, AuthSideEffect>(AuthUiState())

Expand Down Expand Up @@ -81,12 +82,9 @@ class AuthViewModel @Inject constructor(

private fun autoLogin() {
viewModelScope.launch {
dataStoreRepository.getString(DataStoreKey.ACCESS_TOKEN)
.collect {
if (it.isNotEmpty()) {
loginStateP.postValue(LoginState.LOGIN_SUCCESS)
}
}
autoLoginUseCase().let {
loginStateP.postValue(it)
}
}
}

Expand All @@ -105,8 +103,8 @@ class AuthViewModel @Inject constructor(
classNumber = state.classNumber,
gender = state.gender,
consents = Consents(
marketing = state.consents[3]
)
marketing = state.consents[3],
),
),
)

Expand Down

0 comments on commit 952078b

Please sign in to comment.