Skip to content

Commit

Permalink
feat: #10 로그인 기능 구현
Browse files Browse the repository at this point in the history
  • Loading branch information
Hyobeen-Park committed May 3, 2024
1 parent d968644 commit ec0ae9b
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
36 changes: 36 additions & 0 deletions app/src/main/java/com/sopt/now/compose/LoginActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,18 @@ import androidx.compose.ui.text.style.TextAlign
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import androidx.compose.ui.unit.sp
import androidx.lifecycle.MutableLiveData
import com.sopt.now.compose.data.DTO.request.RequestLoginDto
import com.sopt.now.compose.data.DTO.response.ResponseSignupDto
import com.sopt.now.compose.data.Key.ID
import com.sopt.now.compose.data.Key.MBTI
import com.sopt.now.compose.data.Key.NICKNAME
import com.sopt.now.compose.data.Key.PW
import com.sopt.now.compose.data.ServicePool
import com.sopt.now.compose.ui.theme.NOWSOPTAndroidTheme
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class LoginActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
Expand Down Expand Up @@ -70,6 +77,9 @@ fun Login(signupId: String?, signupPw: String?, nickname: String?, mbti: String?
var id by remember { mutableStateOf("") }
var pw by remember { mutableStateOf("") }

val authService by lazy { ServicePool.authService }
val liveData = MutableLiveData<SignupState>()

var shouldShowPassword by remember {
mutableStateOf(false)
}
Expand Down Expand Up @@ -150,6 +160,32 @@ fun Login(signupId: String?, signupPw: String?, nickname: String?, mbti: String?
)
Button(
onClick = {
val requestLoginDto = RequestLoginDto(id, pw)
authService.login(requestLoginDto).enqueue(object : Callback<ResponseSignupDto> {
override fun onResponse(
call: Call<ResponseSignupDto>,
response: Response<ResponseSignupDto>
) {
if (response.isSuccessful) {
val data: ResponseSignupDto? = response.body()
val userId = response.headers()["location"]

val intent = Intent(context, MainActivity::class.java)
intent.putExtra("userId", userId)

Toast.makeText(context, "$userId 님 로그인에 성공했습니다", Toast.LENGTH_SHORT).show()
context.startActivity(intent)
} else {
liveData.value = SignupState(true, "아이디와 비밀번호가 일치하지 않습니다")
}
}

override fun onFailure(call: Call<ResponseSignupDto>, t: Throwable) {
liveData.value = SignupState(true, "서버 통신 에러")
}
})


if (id == signupId && pw == signupPw) {
Toast.makeText(context, R.string.login_success, Toast.LENGTH_SHORT).show()
Intent(context, MainActivity::class.java).apply {
Expand Down
6 changes: 6 additions & 0 deletions app/src/main/java/com/sopt/now/compose/data/AuthService.kt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.sopt.now.compose.data

import com.sopt.now.compose.data.DTO.request.RequestLoginDto
import com.sopt.now.compose.data.DTO.request.RequestSignupDto
import com.sopt.now.compose.data.DTO.response.ResponseSignupDto
import retrofit2.Call
Expand All @@ -11,4 +12,9 @@ interface AuthService {
fun signup(
@Body request: RequestSignupDto,
): Call<ResponseSignupDto>

@POST("member/login")
fun login(
@Body request: RequestLoginDto,
): Call<ResponseSignupDto>
}

0 comments on commit ec0ae9b

Please sign in to comment.