Skip to content

Commit

Permalink
Merge pull request #50 from mmihye/feature/#45
Browse files Browse the repository at this point in the history
[#45] 닉네임 중복체크 API
  • Loading branch information
mmihye authored Jun 25, 2023
2 parents e1a98a1 + e89085e commit 6a5b314
Show file tree
Hide file tree
Showing 17 changed files with 307 additions and 26 deletions.
5 changes: 5 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,15 @@ dependencies {
//naver Map API
implementation 'com.naver.maps:map-sdk:3.16.1'


// Kakao Login
implementation 'com.kakao.sdk:v2-user:2.14.0'

//retrofit
implementation 'com.squareup.retrofit2:retrofit:2.9.0'
implementation 'com.squareup.retrofit2:converter-gson:2.9.0'
implementation 'com.google.code.gson:gson:2.9.0'
implementation 'com.squareup.okhttp3:logging-interceptor:3.8.0'


}
15 changes: 14 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,14 @@
xmlns:tools="http://schemas.android.com/tools"
package="com.android.example.travalue">


<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>


<application
android:name="com.android.example.travalue.GlobalApplication"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
Expand Down Expand Up @@ -43,6 +46,16 @@
android:value="" />
<nav-graph android:value="@navigation/nav_graph" />
</activity>
</application>

<activity android:name="com.kakao.sdk.auth.AuthCodeHandlerActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
<data android:host="oauth"
android:scheme="@string/kakao_native_key" />
</intent-filter>
</activity>
</application>
</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.android.example.travalue

import android.app.Application
import com.kakao.sdk.common.KakaoSdk

class GlobalApplication : Application() {
override fun onCreate() {
super.onCreate()
// 다른 초기화 코드들

KakaoSdk.init(this, getString(R.string.native_key))
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.android.example.travalue

import android.content.ContentValues.TAG
import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.fragment.app.Fragment
Expand All @@ -10,6 +12,8 @@ import androidx.navigation.fragment.NavHostFragment
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.setupActionBarWithNavController
import com.android.example.travalue.databinding.ActivityMainBinding
import com.kakao.sdk.common.KakaoSdk
import com.kakao.sdk.common.util.Utility


class MainActivity : AppCompatActivity() {
Expand All @@ -22,6 +26,8 @@ class MainActivity : AppCompatActivity() {
binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

Log.d(TAG, "keyhash : ${Utility.getKeyHash(this)}")

//setToolbar()

migrateToolbarNavigation()
Expand Down
15 changes: 15 additions & 0 deletions app/src/main/java/com/android/example/travalue/api/LoginService.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.android.example.travalue.api

import com.android.example.travalue.model.LoginRequestModel
import com.android.example.travalue.model.LoginResponseModel
import retrofit2.Call
import retrofit2.http.Body
import retrofit2.http.GET
import retrofit2.http.POST

interface LoginService {

@POST("/v1/auth/login")
fun loginRequest(@Body loginRequestData: LoginRequestModel): Call<LoginResponseModel>

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package com.android.example.travalue.apiManager

import android.content.Context
import android.util.Log
import com.android.example.travalue.api.LoginService
import com.android.example.travalue.model.LoginRequestModel
import com.android.example.travalue.model.LoginResponseModel
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import retrofit2.http.Body

class LoginApiManager {
private var retrofit: Retrofit? = null
private var retrofitService: LoginService? = null

companion object { // DCL 적용한 싱글톤 구현
var instance: LoginApiManager? = null
fun getInstance(context: Context?): LoginApiManager? {
if (instance == null) {
@Synchronized
if (instance == null)
instance = LoginApiManager()
}
return instance
}
}

init {
retrofit = Retrofit.Builder()
.baseUrl("http://13.125.137.16:8080")
.addConverterFactory(GsonConverterFactory.create())
.build()

retrofitService = retrofit?.create(LoginService::class.java)
}

fun loginRequest(@Body loginRequestData: LoginRequestModel){
val resultData: Call<LoginResponseModel>? = retrofitService?.loginRequest(loginRequestData)
resultData?.enqueue(object : Callback<LoginResponseModel> {
override fun onResponse(
call: Call<LoginResponseModel>,
response: Response<LoginResponseModel>
) {
if (response.isSuccessful) {
val result: LoginResponseModel = response.body()!!
Log.d("[loignRequest]", "서버응답 : $result")
} else {
Log.d("[loignRequest]", "실패코드_${response.code()}")
}
}

override fun onFailure(call: Call<LoginResponseModel>, t: Throwable) {
t.printStackTrace()
Log.d("[loignRequest]","통신 실패")
}
})
}


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.android.example.travalue.model

data class LoginRequestModel(
val uniqueId: Long?,
val email:String?,
val profileImage:String?,
val socialType:String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.android.example.travalue.model

data class LoginResponseModel(
val token:String,
val userId:Long
)
Original file line number Diff line number Diff line change
@@ -1,15 +1,45 @@
package com.android.example.travalue.ui.info.info

import android.util.Log
import com.android.example.travalue.MainActivity
import com.android.example.travalue.R
import com.android.example.travalue.apiManager.LoginApiManager
import com.android.example.travalue.base.BaseFragment
import com.android.example.travalue.databinding.FragmentLoginBinding
import com.android.example.travalue.model.LoginRequestModel
import com.kakao.sdk.auth.AuthApiClient
import com.kakao.sdk.auth.model.OAuthToken
import com.kakao.sdk.common.model.ClientError
import com.kakao.sdk.common.model.ClientErrorCause
import com.kakao.sdk.common.model.KakaoSdkError
import com.kakao.sdk.user.UserApiClient

import com.kakao.sdk.common.util.Utility


class LoginFragment : BaseFragment<FragmentLoginBinding>(R.layout.fragment_login) {

val apiManager = LoginApiManager.getInstance(context)


// 이메일 로그인 콜백
private val mCallback: (OAuthToken?, Throwable?) -> Unit = { token, error ->
if (error != null) {
Log.e("mihye", "로그인 실패 $error")
} else if (token != null) {
Log.i("mihye", "로그인 성공 ${token.accessToken}")
}
}



override fun initStartView() {
super.initStartView()
(activity as MainActivity).setToolbarTitle("none")


val keyHash = context?.let { Utility.getKeyHash(it) }
Log.i("mihye","keyHash ${keyHash}")
}

override fun initDataBinding() {
Expand All @@ -19,11 +49,120 @@ class LoginFragment : BaseFragment<FragmentLoginBinding>(R.layout.fragment_login
}



override fun initAfterBinding() {
super.initAfterBinding()

binding.btnLogin.setOnClickListener {
navController.navigate(R.id.action_loginFragment_to_registerFragment)
//kakao 로그인
binding.btnLoginKakao.setOnClickListener {
// navController.navigate(R.id.action_loginFragment_to_registerFragment)

// 토큰 확인
if (AuthApiClient.instance.hasToken()) {
Log.i("mihye","token ok")
UserApiClient.instance.accessTokenInfo { _, error ->
if (error != null) {
if (error is KakaoSdkError && error.isInvalidTokenError()) {
//로그인 필요
// 카카오톡 설치 확인
if (context?.let { it1 -> UserApiClient.instance.isKakaoTalkLoginAvailable(it1) } == true) {
// 카카오톡 로그인
context?.let { it1 ->
UserApiClient.instance.loginWithKakaoTalk(it1) { token, error ->
// 로그인 실패 부분
if (error != null) {
Log.e("mihye", "로그인 실패 $error")
// 사용자가 취소
if (error is ClientError && error.reason == ClientErrorCause.Cancelled ) {
return@loginWithKakaoTalk
}
// 다른 오류
else {
context?.let { it1 -> UserApiClient.instance.loginWithKakaoAccount(it1, callback = mCallback) } // 카카오 이메일 로그인
}
}
// 로그인 성공 부분
else if (token != null) {
Log.i("mihye", "로그인 성공 ${token.accessToken}")
}
}
}
} else {
context?.let { it1 -> UserApiClient.instance.loginWithKakaoAccount(it1, callback = mCallback) } // 카카오 이메일 로그인
}
}
else {
//기타 에러
}
}
else {
//토큰 유효성 체크 성공(필요 시 토큰 갱신됨)
UserApiClient.instance.me { user, error ->
if (error != null) {
Log.e("mihye", "사용자 정보 요청 실패 $error")
} else if (user != null) {
Log.i("mihye", "사용자 정보 요청 성공 : $user")
UserApiClient.instance.me { user, error ->
if (error != null) {
Log.e("mihye", "사용자 정보 요청 실패 $error")
} else if (user != null) {
Log.i("mihye", "사용자 정보 요청 성공 : $user")
val loginRequestModel = LoginRequestModel(user.id, user.kakaoAccount?.email,
user.kakaoAccount?.profile?.profileImageUrl, "KAKAO" )
apiManager?.loginRequest(loginRequestModel)
Log.i("mihye", "사용자 정보 서버 전송 성공 : $loginRequestModel")
}
}

}
}
}
}
}
else {
Log.i("mihye"," token no")
//로그인 필요
// 카카오톡 설치 확인
if (context?.let { it1 -> UserApiClient.instance.isKakaoTalkLoginAvailable(it1) } == true) {
// 카카오톡 로그인
context?.let { it1 ->
UserApiClient.instance.loginWithKakaoTalk(it1) { token, error ->
// 로그인 실패 부분
if (error != null) {
Log.e("mihye", "로그인 실패 $error")
// 사용자가 취소
if (error is ClientError && error.reason == ClientErrorCause.Cancelled ) {
return@loginWithKakaoTalk
}
// 다른 오류
else {
context?.let { it1 -> UserApiClient.instance.loginWithKakaoAccount(it1, callback = mCallback) } // 카카오 이메일 로그인
}
}
// 로그인 성공 부분
else if (token != null) {
Log.i("mihye", "로그인 성공 ${token.accessToken}")
UserApiClient.instance.me { user, error ->
if (error != null) {
Log.e("mihye", "사용자 정보 요청 실패 $error")
} else if (user != null) {
Log.i("mihye", "사용자 정보 요청 성공 : $user")
val loginRequestModel = LoginRequestModel(user.id, user.kakaoAccount?.email,
user.kakaoAccount?.profile?.profileImageUrl, "KAKAO" )
apiManager?.loginRequest(loginRequestModel)
Log.i("mihye", "사용자 정보 서버 전송 성공 : $loginRequestModel")
}
}

}
}
}
} else {
context?.let { it1 -> UserApiClient.instance.loginWithKakaoAccount(it1, callback = mCallback) } // 카카오 이메일 로그인
}
}


}
}
}
Binary file removed app/src/main/res/drawable-v24/kakao_logo.png
Binary file not shown.
Binary file removed app/src/main/res/drawable-v24/primary_logo.png
Binary file not shown.
10 changes: 10 additions & 0 deletions app/src/main/res/drawable/kakao_logo.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="13dp"
android:height="12dp"
android:viewportWidth="13"
android:viewportHeight="12">
<path
android:pathData="M6.5,0C3.098,0 0,2.223 0,4.966C0,6.671 1.125,8.175 2.839,9.069L2.118,11.674C2.054,11.905 2.321,12.088 2.525,11.955L5.686,9.892C5.953,9.917 6.224,9.932 6.5,9.932C10.09,9.932 13,7.708 13,4.966C13,2.223 10.09,0 6.5,0Z"
android:fillColor="#000000"
android:fillType="evenOdd"/>
</vector>
Loading

0 comments on commit 6a5b314

Please sign in to comment.