Skip to content

Commit

Permalink
Merge pull request #78 from Team-Umbba/feat/#72
Browse files Browse the repository at this point in the history
[Feat/#72] 카카오 로그인 프로젝트 세팅 및 API 연결
  • Loading branch information
yeoncheong authored Jul 18, 2023
2 parents 330f83f + d9231a1 commit d5e2698
Show file tree
Hide file tree
Showing 22 changed files with 422 additions and 17 deletions.
11 changes: 10 additions & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ android {
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"

buildConfigField "String", "UMBBA_BASE_URL", properties["UMBBA_BASE_URL"]
buildConfigField "String", "KAKAO_NATIVE_APP_KEY", properties["KAKAO_NATIVE_APP_KEY"]
manifestPlaceholders["NATIVE_APP_KEY"] = properties["KAKAO_NATIVE_APP_KEY_NO_QUOTES"]
}

buildTypes {
Expand All @@ -42,9 +44,9 @@ android {
jvmTarget = '17'
}
buildFeatures {
buildConfig true
viewBinding true
dataBinding true
buildConfig true
}
}

Expand Down Expand Up @@ -118,4 +120,11 @@ dependencies {
implementation 'com.google.firebase:firebase-analytics-ktx'
// Import Firebase Cloud Messaging SDK
implementation 'com.google.firebase:firebase-messaging'

// Kakaologin
implementation 'com.kakao.sdk:v2-user:2.11.0'
implementation 'com.kakao.sdk:v2-share:2.11.0'

// EncryptedSharedPreferences
implementation "androidx.security:security-crypto:1.0.0"
}
6 changes: 5 additions & 1 deletion app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,8 @@

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
#-renamesourcefileattribute SourceFile

# https://developers.kakao.com/docs/latest/en/getting-started/sdk-android#configure-for-shrinking-and-obfuscation-(optional)
-keep class com.kakao.sdk.**.model.* { <fields>; }
-keep class * extends com.google.gson.TypeAdapter
15 changes: 15 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,21 @@
android:theme="@style/Theme.Umbbaandroid"
android:usesCleartextTraffic="true"
tools:targetApi="31">
<activity
android:name="com.kakao.sdk.auth.AuthCodeHandlerActivity"
android:exported="true"
android:launchMode="singleTask">
<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="kakao${NATIVE_APP_KEY}" />
</intent-filter>
</activity>
<activity
android:name=".presentation.splash.OnboardingActivity"
android:exported="false"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,23 @@ package com.sopt.umbba_android.application

import android.app.Application
import androidx.appcompat.app.AppCompatDelegate
import com.kakao.sdk.common.KakaoSdk
import com.sopt.umbba_android.BuildConfig.KAKAO_NATIVE_APP_KEY
import com.sopt.umbba_android.data.local.SharedPreferences

class UmbbaApplication: Application() {
override fun onCreate() {
super.onCreate()
AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
initKakao()
setupSharedPreferences()
}

private fun initKakao() {
KakaoSdk.init(this, KAKAO_NATIVE_APP_KEY)
}

private fun setupSharedPreferences() {
SharedPreferences.init(this)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.sopt.umbba_android.data.datasource

import com.sopt.umbba_android.data.model.ServicePool
import com.sopt.umbba_android.data.model.request.LoginRequestDto
import com.sopt.umbba_android.data.model.response.LoginResponseDto

class LoginRemoteDataSource {
private val loginService = ServicePool.loginService
suspend fun postLogin(loginRequestDto: LoginRequestDto) : LoginResponseDto =
loginService.postLogin(loginRequestDto)
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
package com.sopt.umbba_android.data.interceptor

import android.util.Log
import com.sopt.umbba_android.data.local.SharedPreferences
import com.sopt.umbba_android.presentation.login.LoginActivity.Companion.USER_KAKAO_TOKEN
import com.sopt.umbba_android.presentation.login.LoginActivity.Companion.USER_TOKEN
import okhttp3.Interceptor
import okhttp3.Response

class AuthInterceptor : Interceptor {
private val token ="d"

override fun intercept(chain: Interceptor.Chain): Response {

val token = if (SharedPreferences.getString(USER_TOKEN).isNullOrBlank()) {
SharedPreferences.getKakaoString(USER_KAKAO_TOKEN)
// 저장된 서버 액세스 토큰이 없다면 카카오 토큰 전송 -> 회원가입
} else {
"Bearer ${SharedPreferences.getString(USER_TOKEN)}"
// 저장된 서버 액세스 토큰이 있다면 Bearer 붙여서 전송 -> 로그인
}

val originalRequest = chain.request()

val headerRequest = originalRequest.newBuilder() // 헤더를 추가한 req
.header(
"Authorization",
"Bearer $token"
"$token"
)
.build()

Log.e("yeonjin", "헤더 $token")
return chain.proceed(headerRequest)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
package com.sopt.umbba_android.data.local

import android.content.Context
import android.content.SharedPreferences
import androidx.core.content.edit
import androidx.security.crypto.EncryptedSharedPreferences
import androidx.security.crypto.MasterKeys
import com.sopt.umbba_android.BuildConfig
import com.sopt.umbba_android.presentation.login.LoginActivity

const val FILE_NAME = "UMBBA"

object SharedPreferences {
private lateinit var preferences: SharedPreferences

fun init(context: Context) {
val masterKeyAlias = MasterKeys.getOrCreate(MasterKeys.AES256_GCM_SPEC)
preferences =
if (BuildConfig.DEBUG) {
context.getSharedPreferences(FILE_NAME, Context.MODE_PRIVATE)
} else {
EncryptedSharedPreferences.create(
FILE_NAME,
masterKeyAlias,
context,
EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV,
EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM
)
}
}

fun setKakaoString(key: String, value: String?) {
preferences.edit { putString(key, value) }
}

fun getKakaoString(key: String): String? {
return preferences.getString(key, null)
}

//토큰 저장
fun setString(key: String, value: String?) {
preferences.edit { putString(key, value) }
}
//토큰 가져오기 - 자동 로그인 확인
fun getString(key: String): String? {
return preferences.getString(key, null)
}
//온보딩 완료 상태 저장
fun setBoolean(key: String, value: Boolean) {
preferences.edit { putBoolean(key, value) }
}
//온보딩 완료했는지 가져오기 - 온보딩 완료 확인
fun getBoolean(key: String): Boolean {
return preferences.getBoolean(key, false)
}

private fun clear() {
preferences.edit { clear() }
}

fun clearForLogout() {
clear()
setBoolean(LoginActivity.DID_USER_CLEAR_ONBOARD, true)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.sopt.umbba_android.data.service.OnBoardingService
import com.sopt.umbba_android.data.service.QuestionAnswerService
import com.sopt.umbba_android.data.service.SettingService
import com.sopt.umbba_android.data.interceptor.AuthInterceptor
import com.sopt.umbba_android.data.service.LoginService
import kotlinx.serialization.json.Json
import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
Expand Down Expand Up @@ -40,4 +41,5 @@ object ServicePool {
val homeService = ApiFactory.create<HomeService>()
val onBoardingService = ApiFactory.create<OnBoardingService>()
val listService = ApiFactory.create<ListService>()
val loginService = ApiFactory.create<LoginService>()
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.sopt.umbba_android.data.model.request

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class LoginRequestDto (
@SerialName("social_platform")
val socialPlatform : String,
@SerialName("fcm_token")
val fcmToken : String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.sopt.umbba_android.data.model.response

import kotlinx.serialization.SerialName
import kotlinx.serialization.Serializable

@Serializable
data class LoginResponseDto(
@SerialName("status")
val status: Int,
@SerialName("message")
val message: String,
@SerialName("data")
val data: LoginData
) {
@Serializable
data class LoginData(
@SerialName("user_id")
val userId: Long,
@SerialName("is_new_user")
val isNewUser: Boolean,
@SerialName("username")
val username: String?,
@SerialName("gender")
val gender: String?,
@SerialName("born_year")
val bornYear: Int?,
@SerialName("token_dto")
val tokenDto: TokenData,
@SerialName("fcm_token")
val fcmToken: String,
@SerialName("social_platform")
val socialPlatform: String,
@SerialName("social_nickname")
val socialNickname: String,
@SerialName("social_profile_image")
val socialProfileImage: String,
@SerialName("social_access_token")
val socialAccessToken: String
) {
@Serializable
data class TokenData(
@SerialName("access_token")
val accessToken: String,
@SerialName("refresh_token")
val refreshToken: String
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.sopt.umbba_android.data.repository

import android.util.Log
import com.sopt.umbba_android.data.datasource.LoginRemoteDataSource
import com.sopt.umbba_android.data.model.request.LoginRequestDto
import com.sopt.umbba_android.data.model.response.LoginResponseDto
import com.sopt.umbba_android.domain.repository.LoginRepository
import timber.log.Timber

class LoginRepositoryImpl(
private val loginRemoteDataSource: LoginRemoteDataSource
) : LoginRepository {
override suspend fun postLogin(loginRequestDto: LoginRequestDto): Result<LoginResponseDto> =
runCatching {
loginRemoteDataSource.postLogin(loginRequestDto)
}.onSuccess {
Log.e("yeonjin", "Impl login 실패")
}.onFailure {
Log.e("yeonjin", "Impl login 실패")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.sopt.umbba_android.data.service

import com.sopt.umbba_android.data.model.request.LoginRequestDto
import com.sopt.umbba_android.data.model.response.LoginResponseDto
import retrofit2.http.Body
import retrofit2.http.Header
import retrofit2.http.POST

interface LoginService {

@POST("/login")
suspend fun postLogin(
@Body body: LoginRequestDto
): LoginResponseDto
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.sopt.umbba_android.domain.repository

import com.sopt.umbba_android.data.model.request.LoginRequestDto
import com.sopt.umbba_android.data.model.response.LoginResponseDto

interface LoginRepository {
suspend fun postLogin(loginRequestDto: LoginRequestDto) : Result<LoginResponseDto>
}
Loading

0 comments on commit d5e2698

Please sign in to comment.