From e75c7014a401908e5eaef2560677ca3de88c7746 Mon Sep 17 00:00:00 2001 From: BAEK0111 Date: Tue, 25 Jun 2024 01:15:13 +0900 Subject: [PATCH] =?UTF-8?q?[simba/#47]=20feat::=20Login=20API=20=EC=97=B0?= =?UTF-8?q?=EB=8F=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../main/java/com/example/umc_6th/AuthApi.kt | 3 ++ .../java/com/example/umc_6th/AuthService.kt | 53 +++++++++++++++++++ .../java/com/example/umc_6th/BaseResponse.kt | 14 +++-- .../java/com/example/umc_6th/LoginActivity.kt | 30 +++++++---- .../java/com/example/umc_6th/LoginView.kt | 6 +++ .../java/com/example/umc_6th/MainActivity.kt | 7 +++ .../com/example/umc_6th/SignUpActivity.kt | 31 +++++------ .../java/com/example/umc_6th/SignUpView.kt | 6 +++ 8 files changed, 118 insertions(+), 32 deletions(-) create mode 100644 UMC_6th/app/src/main/java/com/example/umc_6th/AuthService.kt create mode 100644 UMC_6th/app/src/main/java/com/example/umc_6th/LoginView.kt create mode 100644 UMC_6th/app/src/main/java/com/example/umc_6th/SignUpView.kt diff --git a/UMC_6th/app/src/main/java/com/example/umc_6th/AuthApi.kt b/UMC_6th/app/src/main/java/com/example/umc_6th/AuthApi.kt index 9d75a39..61ba302 100644 --- a/UMC_6th/app/src/main/java/com/example/umc_6th/AuthApi.kt +++ b/UMC_6th/app/src/main/java/com/example/umc_6th/AuthApi.kt @@ -7,4 +7,7 @@ import retrofit2.http.POST interface AuthApi { @POST("/users") fun signUp(@Body user : User) : Call + + @POST("/users/login") + fun login(@Body user : User) : Call } \ No newline at end of file diff --git a/UMC_6th/app/src/main/java/com/example/umc_6th/AuthService.kt b/UMC_6th/app/src/main/java/com/example/umc_6th/AuthService.kt new file mode 100644 index 0000000..3f04204 --- /dev/null +++ b/UMC_6th/app/src/main/java/com/example/umc_6th/AuthService.kt @@ -0,0 +1,53 @@ +package com.example.umc_6th + +import android.util.Log +import retrofit2.Call +import retrofit2.Callback +import retrofit2.Response + +class AuthService { + private lateinit var signUpView: SignUpView + private lateinit var loginView : LoginView + + fun setSignUpView(signUpView: SignUpView) { + this.signUpView = signUpView + } + + fun setLoginView(loginView: LoginView) { + this.loginView = loginView + } + + + fun signUp(user : User) { + RetrofitInstance.authApi.signUp(user).enqueue(object: Callback { + override fun onResponse(call: Call, response: Response) { + Log.d("SignUp-Success", response.toString()) + val response : BaseResponse = response.body()!! + when(response.code) { + 1000 -> signUpView.onSignUpSuccess() + else -> signUpView.onSignUpFailure(response.message) + } + } + override fun onFailure(call: Call, t: Throwable) { + Log.d("SignUp-Failure", t.message.toString()) + } + }) + Log.d("SignUpActivity", "All Finished") + } + fun login(user : User) { + RetrofitInstance.authApi.login(user).enqueue(object: Callback { + override fun onResponse(call: Call, response: Response) { + Log.d("Login-Success", response.toString()) + val response : BaseResponse = response.body()!! + when(val code = response.code) { + 1000 -> loginView.onLoginSuccess(code, response.result!!) + else -> loginView.onLoginFailure(response.message) + } + } + override fun onFailure(call: Call, t: Throwable) { + Log.d("Login-Failure", t.message.toString()) + } + }) + Log.d("LoginActivity", "All Finished") + } +} \ No newline at end of file diff --git a/UMC_6th/app/src/main/java/com/example/umc_6th/BaseResponse.kt b/UMC_6th/app/src/main/java/com/example/umc_6th/BaseResponse.kt index 2ce028f..c642468 100644 --- a/UMC_6th/app/src/main/java/com/example/umc_6th/BaseResponse.kt +++ b/UMC_6th/app/src/main/java/com/example/umc_6th/BaseResponse.kt @@ -1,7 +1,15 @@ package com.example.umc_6th +import com.google.gson.annotations.SerializedName + data class BaseResponse( - val isSuccess : Boolean, - val code : Int, - val message : String + @SerializedName("isSuccess") val isSuccess: Boolean, + @SerializedName("code") val code: Int, + @SerializedName("message") val message: String, + @SerializedName("result") val result: Result? ) + +data class Result ( + @SerializedName("userIdx") var userIdx : Int, + @SerializedName("jwt") var jwt : String +) \ No newline at end of file diff --git a/UMC_6th/app/src/main/java/com/example/umc_6th/LoginActivity.kt b/UMC_6th/app/src/main/java/com/example/umc_6th/LoginActivity.kt index 8052100..1ba3a04 100644 --- a/UMC_6th/app/src/main/java/com/example/umc_6th/LoginActivity.kt +++ b/UMC_6th/app/src/main/java/com/example/umc_6th/LoginActivity.kt @@ -7,7 +7,7 @@ import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import com.example.umc_6th.databinding.ActivityLoginBinding -class LoginActivity:AppCompatActivity() { +class LoginActivity:AppCompatActivity(), LoginView { lateinit var binding : ActivityLoginBinding @@ -38,16 +38,10 @@ class LoginActivity:AppCompatActivity() { val email : String = binding.loginIdEt.text.toString() + "@" + binding.loginDirectInputEt.text.toString() val pwd : String = binding.loginPasswordEt.text.toString() - val songDB = SongDatabase.getInstance(this)!! - val user = songDB.userDao().getUser(email, pwd) + val authService = AuthService() + authService.setLoginView(this) - if (user != null) { - Log.d("LoginActivity", user.id.toString()) - saveJwt(user.id) - startMainActivity() - } else { - Toast.makeText(this, "회원 정보가 존재하지 않습니다", Toast.LENGTH_SHORT).show() - } + authService.login(User(email, pwd, "")) } private fun startMainActivity() { @@ -62,4 +56,20 @@ class LoginActivity:AppCompatActivity() { editor.apply() } + private fun saveJwtFromServer(jwt : String) { + val spf = getSharedPreferences("auth2", MODE_PRIVATE) + val editor = spf.edit() + + editor.putString("jwt", jwt) + editor.apply() + } + + override fun onLoginSuccess(code: Int, result: Result) { + saveJwtFromServer(result.jwt) + startMainActivity() + } + override fun onLoginFailure(message: String) { + + } + } \ No newline at end of file diff --git a/UMC_6th/app/src/main/java/com/example/umc_6th/LoginView.kt b/UMC_6th/app/src/main/java/com/example/umc_6th/LoginView.kt new file mode 100644 index 0000000..ec0c20f --- /dev/null +++ b/UMC_6th/app/src/main/java/com/example/umc_6th/LoginView.kt @@ -0,0 +1,6 @@ +package com.example.umc_6th + +interface LoginView { + fun onLoginSuccess(code : Int, result : Result) + fun onLoginFailure(message : String) +} \ No newline at end of file diff --git a/UMC_6th/app/src/main/java/com/example/umc_6th/MainActivity.kt b/UMC_6th/app/src/main/java/com/example/umc_6th/MainActivity.kt index 89b9be8..9649418 100644 --- a/UMC_6th/app/src/main/java/com/example/umc_6th/MainActivity.kt +++ b/UMC_6th/app/src/main/java/com/example/umc_6th/MainActivity.kt @@ -54,6 +54,13 @@ class MainActivity : AppCompatActivity() { startActivity(intent) activityResultLauncher.launch(intent) } + Log.d("MainActivity",getJwt().toString()) + } + + private fun getJwt(): String? { + val spf = this.getSharedPreferences("auth2", MODE_PRIVATE) + + return spf!!.getString("jwt","") } override fun onResume() { diff --git a/UMC_6th/app/src/main/java/com/example/umc_6th/SignUpActivity.kt b/UMC_6th/app/src/main/java/com/example/umc_6th/SignUpActivity.kt index f6b05ae..6b89b76 100644 --- a/UMC_6th/app/src/main/java/com/example/umc_6th/SignUpActivity.kt +++ b/UMC_6th/app/src/main/java/com/example/umc_6th/SignUpActivity.kt @@ -10,7 +10,7 @@ import retrofit2.Call import retrofit2.Callback import retrofit2.Response -class SignUpActivity : AppCompatActivity(){ +class SignUpActivity : AppCompatActivity(), SignUpView{ lateinit var binding : ActivitySignupBinding @@ -47,26 +47,19 @@ class SignUpActivity : AppCompatActivity(){ return false } - RetrofitInstance.authApi.signUp(getUser()).enqueue(object: Callback { - override fun onResponse(call: Call, response: Response) { - Log.d("SignUp-Success", response.toString()) - val response : BaseResponse = response.body()!! - when(response.code) { - 1000 -> finish() // 성공 - 2016, 2018 -> { - binding.signUpEmailErrorTv.visibility = View.VISIBLE - binding.signUpEmailErrorTv.text = response.message - } - } + val authService = AuthService() + authService.setSignUpView(this) // 객체를 통한 멤버 함수 호출 - } + authService.signUp(getUser()) + return true + } - override fun onFailure(call: Call, t: Throwable) { - Log.d("SignUp-Failure", t.message.toString()) - } - }) - Log.d("SignUpActivity", "All Finished") + override fun onSignUpSuccess() { + finish() + } - return true + override fun onSignUpFailure(message: String) { + binding.signUpEmailErrorTv.visibility = View.VISIBLE + binding.signUpEmailErrorTv.text = message } } \ No newline at end of file diff --git a/UMC_6th/app/src/main/java/com/example/umc_6th/SignUpView.kt b/UMC_6th/app/src/main/java/com/example/umc_6th/SignUpView.kt new file mode 100644 index 0000000..1d08efc --- /dev/null +++ b/UMC_6th/app/src/main/java/com/example/umc_6th/SignUpView.kt @@ -0,0 +1,6 @@ +package com.example.umc_6th + +interface SignUpView { + fun onSignUpSuccess() + fun onSignUpFailure(message : String) +} \ No newline at end of file