Skip to content

Commit

Permalink
[nunu/#43] feat: 로그인/회원가입 api 연결 구현
Browse files Browse the repository at this point in the history
- Retrofit 이용
- jwt int -> string 변경
  • Loading branch information
Ssamssamukja committed Jun 18, 2024
1 parent fd4ed78 commit 8718d09
Show file tree
Hide file tree
Showing 14 changed files with 184 additions and 9 deletions.
8 changes: 8 additions & 0 deletions UMC_6th/.idea/deploymentTargetSelector.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions UMC_6th/app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,17 @@ dependencies {
implementation("androidx.room:room-ktx:2.6.1")
implementation("androidx.room:room-runtime:2.6.1")
kapt("androidx.room:room-compiler:2.6.1")

//Retrofit
implementation("com.squareup.retrofit2:retrofit:2.9.0")
implementation("com.squareup.retrofit2:converter-gson:2.9.0")
implementation("com.squareup.retrofit2:adapter-rxjava2:2.9.0")

//okHttp
implementation("com.squareup.okhttp3:okhttp:4.9.0")
implementation("com.squareup.okhttp3:logging-interceptor:4.9.0")

//Glide
implementation("com.github.bumptech.glide:glide:4.11.0")
annotationProcessor("com.github.bumptech.glide:compiler:4.11.0")
}
1 change: 1 addition & 0 deletions UMC_6th/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
android:label="FLO"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:usesCleartextTraffic="true"
android:theme="@style/Theme.UMC_6th"
tools:targetApi="31">
<service
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.example.umc_6th

class ApiRepository {
companion object {
const val BASE_URL = "https://edu-api-test.softsquared.com"
}
}
13 changes: 13 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
@@ -0,0 +1,13 @@
package com.example.umc_6th

import retrofit2.Call
import retrofit2.http.Body
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>
}
52 changes: 52 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,52 @@
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")
}
}
15 changes: 15 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/BaseResponse.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.example.umc_6th

import com.google.gson.annotations.SerializedName

data class BaseResponse(
@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
)
Original file line number Diff line number Diff line change
Expand Up @@ -96,14 +96,14 @@ class LockerFragment : Fragment() {
}

// 로그인 또는 로그아웃 기능
private fun getJwt() : Int {
private fun getJwt() : String? {
val spf = requireActivity().getSharedPreferences("auth", AppCompatActivity.MODE_PRIVATE)
return spf!!.getInt("jwt", 0)
return spf!!.getString("jwt", "")
}

private fun initViews() {
val jwt: Int = getJwt()
if (jwt == 0) {
val jwt: String = getJwt().toString()
if (jwt == "") {
binding.txLogin.text="로그인"
binding.txLogin.setOnClickListener{
startActivity(Intent(requireActivity(), LoginActivity::class.java))
Expand Down
22 changes: 18 additions & 4 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 {

private var _binding : ActivityLoginBinding? = null
private val binding get() = _binding!!
Expand Down Expand Up @@ -48,26 +48,40 @@ class LoginActivity : AppCompatActivity() {

if (user != null) {
Log.d("LoginActivity", user.id.toString())
saveJwt(user.id)
saveJwt(user.id.toString())
startMainActivity()
} else {
Toast.makeText(this, "회원 정보가 존재하지 않습니다", Toast.LENGTH_SHORT).show()
}

val authService = AuthService()
authService.setLoginView(this)

authService.login(User(email, pwd, ""))
}

private fun startMainActivity() {
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
}

private fun saveJwt(jwt: Int) {
private fun saveJwt(jwt: String) {
val spf = getSharedPreferences("auth" , MODE_PRIVATE)
val editor = spf.edit()

editor.putInt("jwt", jwt)
editor.putString("jwt", jwt)
editor.apply()
}

override fun onLoginSuccess(code : Int, result : Result) {
saveJwt(result.jwt)
startMainActivity()
}

override fun onLoginFailure(message : String) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show()
}

override fun onDestroy() {
super.onDestroy()
_binding = null
Expand Down
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)
}
6 changes: 6 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 @@ -85,7 +85,13 @@ class MainActivity : AppCompatActivity() {
startActivity(intent)
}

Log.d("MainActivity", getJwt().toString())
}

private fun getJwt() : String? {
val spf = this.getSharedPreferences("auth", MODE_PRIVATE)

return spf!!.getString("jwt", "")
}
//문제없음..
override fun onStart() {
Expand Down
16 changes: 16 additions & 0 deletions UMC_6th/app/src/main/java/com/example/umc_6th/RetrofitInstance.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package com.example.umc_6th

import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory

class RetrofitInstance {
companion object {
private val retrofit by lazy {
Retrofit.Builder()
.baseUrl(ApiRepository.BASE_URL)
.addConverterFactory(GsonConverterFactory.create())
.build()
}
val authApi = retrofit.create(AuthApi::class.java)
}
}
20 changes: 19 additions & 1 deletion UMC_6th/app/src/main/java/com/example/umc_6th/SignUpActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,17 @@ package com.example.umc_6th

import android.os.Bundle
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.example.umc_6th.databinding.ActivityLoginBinding
import com.example.umc_6th.databinding.ActivitySignupBinding
import com.example.umc_6th.User
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response

class SignUpActivity : AppCompatActivity() {
class SignUpActivity : AppCompatActivity(), SignUpView {
lateinit var binding : ActivitySignupBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Expand Down Expand Up @@ -49,7 +53,21 @@ class SignUpActivity : AppCompatActivity() {
Log.d("sign-up", user.toString())

Toast.makeText(this, "회원가입이 완료되었습니다.", Toast.LENGTH_SHORT).show()

val authService = AuthService()
authService.setSignUpView(this) // 객체를 통한 멤버 함수 호출


authService.signUp(getUser())
return true
}
override fun onSignUpSuccess() {
finish()
}

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 8718d09

Please sign in to comment.