Skip to content

Commit

Permalink
#8 [feat] : 로그인 실패 시 안내 토스트
Browse files Browse the repository at this point in the history
  • Loading branch information
gitsuhyun committed Nov 14, 2024
1 parent a29a3e1 commit 07beb20
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
4 changes: 2 additions & 2 deletions app/src/main/java/org/sopt/and/feature/login/LoginScreen.kt
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@ import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.viewmodel.compose.viewModel
import androidx.navigation.NavController
import kotlinx.coroutines.launch
import org.sopt.and.R
import org.sopt.and.core.designsystem.component.AuthTextField
import org.sopt.and.core.designsystem.component.SocialLoginButtonGroup
Expand Down Expand Up @@ -188,15 +187,16 @@ fun LoginScreen(
viewModel.onLoginClick(
onSuccess = { userName, password ->
viewModel.postUserLogin(
context = context,
body = UserLoginRequest(
username = userName,
password = password
),
) { body ->
editor.putString("loginToken", body!!.result.token)
editor.apply()
onLoginSuccess(userName, password)
}
onLoginSuccess(userName, password)
},
)

Expand Down
14 changes: 10 additions & 4 deletions app/src/main/java/org/sopt/and/feature/login/LoginViewModel.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.sopt.and.feature.login

import android.content.Context
import android.util.Log
import androidx.compose.runtime.State
import androidx.compose.runtime.mutableStateOf
Expand All @@ -11,6 +12,7 @@ import kotlinx.coroutines.launch
import org.sopt.and.data.ServicePool
import org.sopt.and.data.model.dto.ResponseUserTokenDto
import org.sopt.and.data.model.request.UserLoginRequest
import org.sopt.and.utils.toast
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
Expand All @@ -21,7 +23,11 @@ class LoginViewModel : ViewModel() {
private val _userState = mutableStateOf<ResponseUserTokenDto?>(null)
val userState: State<ResponseUserTokenDto?> get() = _userState

fun postUserLogin(body: UserLoginRequest, callback: (ResponseUserTokenDto?) -> Unit) {
fun postUserLogin(
context: Context,
body: UserLoginRequest,
callback: (ResponseUserTokenDto?) -> Unit
) {
userService.postUserLogin(
body = body
).enqueue(object : Callback<ResponseUserTokenDto> {
Expand All @@ -36,13 +42,13 @@ class LoginViewModel : ViewModel() {
} else {
val error = response.message()
Log.e("error", error.toString())
callback(null)
context.toast("로그인에 실패했습니다.")
}
}

override fun onFailure(call: Call<ResponseUserTokenDto>, t: Throwable) {
Log.e("failure", t.message.toString())
callback(null)
context.toast("로그인에 실패했습니다.")
}
})
}
Expand All @@ -66,7 +72,7 @@ class LoginViewModel : ViewModel() {
onSuccess: (String, String) -> Unit,
) {
viewModelScope.launch {
onSuccess(userName.value!!, password.value!!)
onSuccess(userName.value!!, password.value!!)
}
}
}

0 comments on commit 07beb20

Please sign in to comment.