Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mod] 자동 로그인, 로그인 로직 수정 #171

Merged
merged 8 commits into from
Sep 20, 2023
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import android.os.Bundle
import androidx.core.splashscreen.SplashScreen.Companion.installSplashScreen
import androidx.lifecycle.lifecycleScope
import com.sopt.geonppang.R
import com.sopt.geonppang.data.datasource.local.GPDataSource
import com.sopt.geonppang.databinding.ActivitySplashBinding
import com.sopt.geonppang.presentation.auth.SignActivity
import com.sopt.geonppang.util.binding.BindingActivity
Expand All @@ -23,13 +24,22 @@ class SplashActivity : BindingActivity<ActivitySplashBinding>(R.layout.activity_
private fun loadSplashScreen() {
lifecycleScope.launch {
delay(1500L)
moveToSign()
setAutoLogin()
finish()
}
}

private fun setAutoLogin() {
val gpDataSource = GPDataSource(this)
if (gpDataSource.isLogin) moveToHome()
else moveToSign()
}

private fun moveToHome() {
startActivity(Intent(this, MainActivity::class.java))
}

private fun moveToSign() {
startActivity(Intent(this, SignActivity::class.java))
finish()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ class AuthViewModel @Inject constructor(
return isValidNickname.value == true
}

fun setAutoLogin() {
gpDataSource.isLogin = true
}

fun signUp(
platformType: PlatformType,
platformToken: String,
Expand Down Expand Up @@ -116,8 +120,6 @@ class AuthViewModel @Inject constructor(
gpDataSource.refreshToken = BEARER_PREFIX + refreshToken
}
_signUpState.value = UiState.Success(true)
Timber.tag("access token").d(gpDataSource.accessToken)
Timber.tag("refresh token").d(gpDataSource.refreshToken)
}
.onFailure { throwable ->
Timber.e(throwable.message)
Expand All @@ -131,6 +133,8 @@ class AuthViewModel @Inject constructor(
authRepository.settingNickname(RequestNicknameSetting(nickname))
.onSuccess {
_signUpState.value = UiState.Success(true)
// 소셜 회원가입 시 자동 로그인 설정
setAutoLogin()
}
.onFailure { throwable ->
Timber.e(throwable.message)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class SignActivity :
}

AuthRoleType.USER -> {
authViewModel.setAutoLogin()
moveToMain()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import androidx.lifecycle.viewModelScope
import com.sopt.geonppang.data.datasource.local.GPDataSource
import com.sopt.geonppang.data.model.request.RequestLogin
import com.sopt.geonppang.domain.repository.AuthRepository
import com.sopt.geonppang.util.UiState
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.asStateFlow
Expand All @@ -19,26 +20,33 @@ class LogInViewModel @Inject constructor(
) : ViewModel() {
val loginEmail = MutableStateFlow("")
val loginPassword = MutableStateFlow("")
private val _loginState = MutableStateFlow<Boolean?>(null)
private val _loginState = MutableStateFlow<UiState<Boolean>>(UiState.Loading)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

중복확인도 uistate 로 변경할까염

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

조하염~~

val loginState get() = _loginState.asStateFlow()

fun initLogin() {
_loginState.value = null
_loginState.value = UiState.Loading
}

fun setAutoLogin() {
gpDataSource.isLogin = true
}

fun login() {
viewModelScope.launch {
authRepository.login(RequestLogin(loginEmail.value, loginPassword.value))
.onSuccess { loginResponse ->
val responseHeader = loginResponse.headers()
val accessToken = responseHeader[AUTHORIZATION].toString()
val accessToken = responseHeader[AUTHORIZATION]
val refreshToken = responseHeader[AUTHORIZATION_REFRESH]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refresh 도 해야하는지 생각을 못했다 .. 🐰 ❤️

if (loginResponse.code() == 200) {
gpDataSource.accessToken = BEARER_PREFIX + accessToken
gpDataSource.refreshToken = BEARER_PREFIX + refreshToken
gpDataSource.isLogin = true
_loginState.value = true
_loginState.value = UiState.Success(true)
setAutoLogin()
}
if (loginResponse.code() == 400) {
_loginState.value = false
_loginState.value = UiState.Error(loginResponse.message())
}
if (loginResponse.code() == 500) {
Timber.tag("서버 오류")
Expand All @@ -51,6 +59,7 @@ class LogInViewModel @Inject constructor(

companion object {
const val AUTHORIZATION = "Authorization"
const val AUTHORIZATION_REFRESH = "Authorization-refresh"
const val BEARER_PREFIX = "Bearer "
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ import androidx.activity.viewModels
import androidx.lifecycle.flowWithLifecycle
import androidx.lifecycle.lifecycleScope
import com.sopt.geonppang.R
import com.sopt.geonppang.data.datasource.local.GPDataSource
import com.sopt.geonppang.databinding.ActivityLoginBinding
import com.sopt.geonppang.presentation.MainActivity
import com.sopt.geonppang.presentation.auth.SignUpActivity
import com.sopt.geonppang.presentation.home.HomeFragment
import com.sopt.geonppang.util.UiState
import com.sopt.geonppang.util.binding.BindingActivity
import com.sopt.geonppang.util.extension.hideKeyboard
import dagger.hilt.android.AndroidEntryPoint
Expand All @@ -25,7 +25,6 @@ class LoginActivity :
binding.viewModel = viewModel
binding.lifecycleOwner = this

autoLogin()
addListener()
collectData()
}
Expand All @@ -45,11 +44,12 @@ class LoginActivity :
private fun collectData() {
viewModel.loginState.flowWithLifecycle(lifecycle).onEach { loginState ->
when (loginState) {
true -> {
is UiState.Success -> {
moveToHome()
viewModel.setAutoLogin()
}

false -> {
is UiState.Error -> {
showLoginFailDialog()
viewModel.initLogin()
}
Expand All @@ -64,20 +64,16 @@ class LoginActivity :
}

private fun moveToHome() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

짱이다

startActivity(Intent(this, HomeFragment::class.java))
val intent = Intent(this, MainActivity::class.java)
intent.flags = Intent.FLAG_ACTIVITY_CLEAR_TOP or Intent.FLAG_ACTIVITY_NEW_TASK
startActivity(intent)
finish()
}

private fun moveToSignup() {
startActivity(Intent(this, SignUpActivity::class.java))
}

private fun autoLogin() {
val gpDataSource = GPDataSource(this)
if (gpDataSource.isLogin)
moveToHome()
}

companion object {
const val LOGIN_FAIL = "loginFail"
}
Expand Down
Loading