Skip to content

Commit

Permalink
[simba/#47] feat:: Login API 연동
Browse files Browse the repository at this point in the history
  • Loading branch information
BAEK0111 committed Jun 24, 2024
1 parent 7b01626 commit e75c701
Show file tree
Hide file tree
Showing 8 changed files with 118 additions and 32 deletions.
3 changes: 3 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/AuthApi.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ import retrofit2.http.POST
interface AuthApi {
@POST("/users")
fun signUp(@Body user : User) : Call<BaseResponse>

@POST("/users/login")
fun login(@Body user : User) : Call<BaseResponse>
}
53 changes: 53 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/AuthService.kt
Original file line number Diff line number Diff line change
@@ -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<BaseResponse> {
override fun onResponse(call: Call<BaseResponse>, response: Response<BaseResponse>) {
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<BaseResponse>, 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<BaseResponse> {
override fun onResponse(call: Call<BaseResponse>, response: Response<BaseResponse>) {
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<BaseResponse>, t: Throwable) {
Log.d("Login-Failure", t.message.toString())
}
})
Log.d("LoginActivity", "All Finished")
}
}
14 changes: 11 additions & 3 deletions UMC_6th/app/src/main/java/com/example/umc_6th/BaseResponse.kt
Original file line number Diff line number Diff line change
@@ -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
)
30 changes: 20 additions & 10 deletions UMC_6th/app/src/main/java/com/example/umc_6th/LoginActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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() {
Expand All @@ -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) {

}

}
6 changes: 6 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/LoginView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.umc_6th

interface LoginView {
fun onLoginSuccess(code : Int, result : Result)
fun onLoginFailure(message : String)
}
7 changes: 7 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
31 changes: 12 additions & 19 deletions UMC_6th/app/src/main/java/com/example/umc_6th/SignUpActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class SignUpActivity : AppCompatActivity(){
class SignUpActivity : AppCompatActivity(), SignUpView{

lateinit var binding : ActivitySignupBinding

Expand Down Expand Up @@ -47,26 +47,19 @@ class SignUpActivity : AppCompatActivity(){
return false
}

RetrofitInstance.authApi.signUp(getUser()).enqueue(object: Callback<BaseResponse> {
override fun onResponse(call: Call<BaseResponse>, response: Response<BaseResponse>) {
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<BaseResponse>, 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
}
}
6 changes: 6 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/SignUpView.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.example.umc_6th

interface SignUpView {
fun onSignUpSuccess()
fun onSignUpFailure(message : String)
}

0 comments on commit e75c701

Please sign in to comment.