Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AN] feat: 소셜 로그인 기능 구현 #235

Merged
merged 32 commits into from
Aug 9, 2023
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
ad222ea
design: LoginActivty에 필요한 drawable source 추가
hyunji1203 Aug 8, 2023
3f08cfc
design: LoginActivty에 필요한 string source 추가
hyunji1203 Aug 8, 2023
1697875
feat: 소셜 로그인을 위한 gradle 추가
hyunji1203 Aug 8, 2023
bb066d3
feat: 난독화 시 카카오 SDK가 포함되지 않도록 규칙 설정
hyunji1203 Aug 8, 2023
d1f7ed0
feat: 카카오 로그인 API key 숨김 처리
hyunji1203 Aug 8, 2023
bce127f
feat: 인터넷 사용 권한 허용
hyunji1203 Aug 8, 2023
31f7faa
feat: 카카오 로그인 기능을 위한 Redirect URI 설정 추가
hyunji1203 Aug 8, 2023
a9e266c
feat: Kakao SDK를 초기화하는 application 구현
hyunji1203 Aug 8, 2023
3bd71ee
feat: Kakao 로그인 기능 Util 구현
hyunji1203 Aug 8, 2023
fa97311
design: LoginActivity 뷰 구현
hyunji1203 Aug 8, 2023
bd35baf
feat: 게임 상태가 진행 중이 아닐 때 Splash 화면에서 로그인 화면으로 이동하도록 구현
hyunji1203 Aug 8, 2023
7e59267
feat: 상태바 색상 변경
hyunji1203 Aug 8, 2023
cff5507
feat: 로그인 기능 구현
hyunji1203 Aug 8, 2023
460efe8
feat: 사용자의 카카오 계정 닉네임을 마이페이지에 보여주는 기능 구현
hyunji1203 Aug 8, 2023
4816d66
feat: 서버와 통신해 토큰을 얻어올 때 필요한 Dto, Domain 객체 생성
hyunji1203 Aug 8, 2023
5c24439
feat: Dto, Domain 변환 mapper 추가
hyunji1203 Aug 8, 2023
a40f690
feat: 서버와 통신하여 로그인 인증을 해주는 retrofit service 구현
hyunji1203 Aug 8, 2023
e7f8eaa
feat: 로그인 인증을 통해 토큰을 가져와주는 레포지토리 구현
hyunji1203 Aug 8, 2023
faf9967
feat: 서버에게 받은 토큰을 저장하는 SharedPreference 구현
hyunji1203 Aug 8, 2023
9bbc81c
feat: 서버에게 토큰을 받아와 로컬 저장소에 저장하는 기능 구현
hyunji1203 Aug 8, 2023
a146f29
feat: 카카오 API KEY 감추기에 따른 yml 파일 수정
hyunji1203 Aug 9, 2023
6bc2bc0
fix: yml 파일 오류 수정
hyunji1203 Aug 9, 2023
23a9bfc
fix: yml 파일 오류 수정
hyunji1203 Aug 9, 2023
407b3b5
fix: yml 파일 오류 수정
hyunji1203 Aug 9, 2023
2ab0d18
fix: yml 파일 오류 수정
hyunji1203 Aug 9, 2023
b19b1f7
refactor: ktlint 적용
hyunji1203 Aug 9, 2023
5cc2224
refactor: yml 파일 코드 정리
hyunji1203 Aug 9, 2023
18ba7c5
refactor: 암호화 처리 되는 EncryptedSharedPreferences로 변경
hyunji1203 Aug 9, 2023
bf96baa
feat: retrofit Header에 서버에서 받아온 토큰 넣어주기 구현
hyunji1203 Aug 9, 2023
2d11ffe
refactor: 카카오 토큰을 함수 인자로 받아오도록 수정
hyunji1203 Aug 9, 2023
73a59ae
rename: preference가 사용된 파일명을 datasource로 변경
hyunji1203 Aug 9, 2023
82a5c97
refactor: 함수 인자명 변경
hyunji1203 Aug 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions android/app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ android {

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
buildConfigField "String", "BASE_URL", properties["BASE_URL"]
buildConfigField "String", "KAKAO_NATIVE_APP_KEY", properties["KAKAO_NATIVE_APP_KEY"]
resValue "string", "kakao_redirection_scheme", properties["kakao_redirection_scheme"]
}

buildTypes {
Expand Down Expand Up @@ -94,4 +96,7 @@ dependencies {
implementation(platform('com.google.firebase:firebase-bom:32.2.0'))
implementation "com.google.firebase:firebase-analytics-ktx"
implementation("com.google.firebase:firebase-crashlytics-ktx")

// kakao Login
implementation "com.kakao.sdk:v2-user:2.15.0"
}
5 changes: 4 additions & 1 deletion android/app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,7 @@

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

-keep class com.kakao.sdk.**.model.* { <fields>; }
-keep class * extends com.google.gson.TypeAdapter
Comment on lines +21 to +24
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

오 이 친구들은 뭔가요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

나중에 저희가 앱 출시하면서 난독화를 진행할 때 카카오 SDK까지 난독화 되지 않도록 막아주는 코드입니다!
카카오 SDK 관련 코드까지 난독화가 되면 해당 기능이 잘 실행되지 않아 API를 사용할 수 없다고 하네요....
그래서 해당 코드 추가했습니다~!

19 changes: 19 additions & 0 deletions android/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />

<application
android:name=".presentation.login.NaagaApplication"
android:allowBackup="true"
android:dataExtractionRules="@xml/data_extraction_rules"
android:fullBackupContent="@xml/backup_rules"
Expand Down Expand Up @@ -56,6 +58,23 @@
<activity
android:name=".presentation.mypage.MyPageActivity"
android:exported="false" />
<activity
android:name=".presentation.login.LoginActivity"
android:exported="false" />
<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_redirection_scheme" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package com.now.naaga.data.local

interface AuthPreference {
fun getAccessToken(): String?
fun setAccessToken(newToken: String)
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

토큰 관리를 추상화한 것 같습니다. 그런데 Preference라는 네이밍은 안드로이드 프레임워크의 SharedPreference를 특정하는 느낌이 강하게 느껴집니다. 추상화라면 특정 기술이 떠오르면 안 될 것 같아요!

일반적으로 데이터 소스라 불리는 기능을 하고 있는 것 같은데 네이밍 후보 중 하나로 고려해봐도 좋을 것 같아요

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

해당 네이밍에 대해서 저도 데이터 소스로 할지 그냥 Preference를 할 지 고민을 많이 했는데요....
역시 빅스의 리뷰를 보니 Datasource로 가는게 맞는 것 같습니다.
그래서 AuthDataSourceKakaoAuthSource로 수정했습니다!

Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.now.naaga.data.local

import android.content.Context
import android.content.SharedPreferences

class KakaoAuthPreference(context: Context): AuthPreference {
private val pref: SharedPreferences =
context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_PRIVATE)

override fun getAccessToken(): String? {
return pref.getString(ACCESS_TOKEN_KEY, "")
}

override fun setAccessToken(newToken: String) {
pref.edit().putString(ACCESS_TOKEN_KEY, newToken).apply()
}

companion object {
private const val PREFERENCE_NAME = "ACCESS_TOKEN_"
private const val ACCESS_TOKEN_KEY = "access_token_key"
}
}
24 changes: 24 additions & 0 deletions android/app/src/main/java/com/now/naaga/data/mapper/AuthMapper.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.now.naaga.data.mapper

import com.now.domain.model.PlatformAuth
import com.now.domain.model.AuthPlatformType
import com.now.domain.model.NaagaAuth
import com.now.naaga.data.remote.dto.NaagaAuthDto
import com.now.naaga.data.remote.dto.PlatformAuthDto

fun PlatformAuthDto.toDomain(): PlatformAuth {
return PlatformAuth(token, AuthPlatformType.findByName(type))
}

fun PlatformAuth.toDto(): PlatformAuthDto {
return PlatformAuthDto(token, type.name)
}

fun NaagaAuthDto.toDomain(): NaagaAuth {
return NaagaAuth(accessToken, refreshToken)
}

fun NaagaAuth.toDto(): NaagaAuthDto {
return NaagaAuthDto(accessToken, refreshToken)
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.now.naaga.data.remote.dto

import kotlinx.serialization.Serializable

@Serializable
data class NaagaAuthDto(
val accessToken: String,
val refreshToken: String?,
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.now.naaga.data.remote.dto

import kotlinx.serialization.Serializable

@Serializable
data class PlatformAuthDto(
val token: String,
val type: String,
)
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package com.now.naaga.data.remote.retrofit

import com.now.naaga.data.remote.retrofit.RetrofitFactory.retrofit
import com.now.naaga.data.remote.retrofit.service.AdventureService
import com.now.naaga.data.remote.retrofit.service.AuthService
import com.now.naaga.data.remote.retrofit.service.PlaceService
import com.now.naaga.data.remote.retrofit.service.RankService
import com.now.naaga.data.remote.retrofit.service.StatisticsService
Expand All @@ -11,4 +12,5 @@ object ServicePool {
val rankService = retrofit.create(RankService::class.java)
val statisticsService = retrofit.create(StatisticsService::class.java)
val placeService = retrofit.create(PlaceService::class.java)
val authService = retrofit.create(AuthService::class.java)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.now.naaga.data.remote.retrofit.service

import com.now.naaga.data.remote.dto.NaagaAuthDto
import com.now.naaga.data.remote.dto.PlatformAuthDto
import retrofit2.Call
import retrofit2.http.Body
import retrofit2.http.POST

interface AuthService {
@POST("/auth")
fun requestToken(
@Body platformAuthDto: PlatformAuthDto,
): Call<NaagaAuthDto>
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.now.naaga.data.repository

import com.now.domain.model.NaagaAuth
import com.now.domain.model.PlatformAuth
import com.now.domain.repository.AuthRepository
import com.now.naaga.data.mapper.toDomain
import com.now.naaga.data.mapper.toDto
import com.now.naaga.data.remote.retrofit.ServicePool
import com.now.naaga.data.remote.retrofit.fetchNaagaResponse

class DefaultAuthRepository : AuthRepository {
override fun getToken(
platformAuth: PlatformAuth,
callback: (Result<NaagaAuth>) -> Unit,
) {
val call = ServicePool.authService.requestToken(platformAuth.toDto())
call.fetchNaagaResponse(
onSuccess = { callback(Result.success(it.toDomain())) },
onFailure = { callback(Result.failure(it)) },
)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package com.now.naaga.presentation.login

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.WindowInsetsControllerCompat
import androidx.lifecycle.ViewModelProvider
import com.now.naaga.R
import com.now.naaga.data.local.KakaoAuthPreference
import com.now.naaga.data.repository.DefaultAuthRepository
import com.now.naaga.databinding.ActivityLoginBinding
import com.now.naaga.presentation.beginadventure.BeginAdventureActivity
import com.now.naaga.util.loginWithKakao

class LoginActivity : AppCompatActivity() {
private lateinit var binding: ActivityLoginBinding
private lateinit var viewModel: LoginViewModel

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityLoginBinding.inflate(layoutInflater)
setContentView(binding.root)
binding.lifecycleOwner = this
initViewModel()
setClickListeners()
setStatusBar()
viewModel.fetchToken()
}

private fun initViewModel() {
val authRepository = DefaultAuthRepository()
val authPreference = KakaoAuthPreference(this)
val factory = LoginViewModelFactory(authRepository, authPreference)
viewModel = ViewModelProvider(this, factory)[LoginViewModel::class.java]
}

private fun setClickListeners() {
binding.ivLoginKakao.setOnClickListener {
loginWithKakao(this) { navigateHome() }
}
}

private fun setStatusBar() {
window.apply {
statusBarColor = getColor(R.color.white)
WindowInsetsControllerCompat(this, this.decorView).isAppearanceLightStatusBars = true
}
}

private fun navigateHome() {
startActivity(BeginAdventureActivity.getIntent(this))
finish()
}

companion object {
fun getIntent(context: Context): Intent {
return Intent(context, LoginActivity::class.java)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package com.now.naaga.presentation.login

import androidx.lifecycle.ViewModel
import com.kakao.sdk.auth.TokenManagerProvider
import com.now.domain.model.AuthPlatformType.KAKAO
import com.now.domain.model.PlatformAuth
import com.now.domain.repository.AuthRepository
import com.now.naaga.data.local.AuthPreference

class LoginViewModel(
private val authRepository: AuthRepository,
private val authPreference: AuthPreference,
) : ViewModel() {
fun fetchToken() {
val token = TokenManagerProvider.instance.manager.getToken()?.accessToken
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

카카오 sdk에 의존성이 생기는 부분인데,액티비티에서 토큰을 받아서 뷰모델의 인자로 넘겨주는 건 어떤가요?
현재 구조에서 뷰모델 테스트가 가능할지 잘 모르겠습니다.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

뷰모델 테스트에 어려움이 생길 것 같다는 의견에 공감합니다!
따라서 액티비티에서 해당 SDK를 사용해 토큰을 얻어내고 그 후 해당 뷰모델 함수의 인자로 얻어낸 토큰을 넣어주는 방식으로 수정했습니다!

token?.let { PlatformAuth(it, KAKAO) }?.let { platformAuth ->
authRepository.getToken(
platformAuth,
callback = { result ->
result
.onSuccess { authPreference.setAccessToken(it.accessToken) }
.onFailure { }
},
)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package com.now.naaga.presentation.login

import androidx.lifecycle.ViewModel
import androidx.lifecycle.ViewModelProvider
import com.now.domain.repository.AuthRepository
import com.now.naaga.data.local.AuthPreference

class LoginViewModelFactory(
private val authRepository: AuthRepository,
private val authPreference: AuthPreference,
) :
ViewModelProvider.Factory {
override fun <T : ViewModel> create(modelClass: Class<T>): T {
if (modelClass.isAssignableFrom(LoginViewModel::class.java)) {
return LoginViewModel(authRepository, authPreference) as T
} else {
throw IllegalArgumentException()
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package com.now.naaga.presentation.login

import android.app.Application
import com.kakao.sdk.common.KakaoSdk
import com.now.naaga.BuildConfig

class NaagaApplication : Application() {
override fun onCreate() {
super.onCreate()
KakaoSdk.init(this, BuildConfig.KAKAO_NATIVE_APP_KEY)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ class MyPageActivity : AppCompatActivity(), AnalyticsDelegate by DefaultAnalytic
viewModel.fetchRank()
viewModel.fetchStatistics()
viewModel.fetchPlaces()
viewModel.fetchNickname()
}

private fun subscribe() {
Expand All @@ -66,7 +67,9 @@ class MyPageActivity : AppCompatActivity(), AnalyticsDelegate by DefaultAnalytic
val placesUiModel = places.map { it.toUiModel() }
binding.customGridMypagePlaces.initContent(placesUiModel)
}

viewModel.nickname.observe(this) { nickname ->
binding.tvMypageNickname.text = nickname
}
viewModel.errorMessage.observe(this) { errorMessage ->
if (NaagaThrowable.ServerConnectFailure().message == errorMessage) {
Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT).show()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package com.now.naaga.presentation.mypage

import android.util.Log
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import com.kakao.sdk.user.UserApiClient
import com.now.domain.model.OrderType
import com.now.domain.model.Place
import com.now.domain.model.Rank
Expand Down Expand Up @@ -30,6 +32,9 @@ class MyPageViewModel(
private val _errorMessage = MutableLiveData<String>()
val errorMessage: LiveData<String> = _errorMessage

private val _nickname = MutableLiveData<String>()
val nickname: LiveData<String> = _nickname

fun fetchRank() {
rankRepository.getMyRank { result: Result<Rank> ->
result
Expand All @@ -54,10 +59,27 @@ class MyPageViewModel(
}
}

fun fetchNickname() {
UserApiClient.instance.me { user, error ->
if (error != null) {
Log.d(KAKAO_USER_INFO_LOG_TAG, KAKAO_USER_INFO_FAIL_MESSAGE + error)
} else if (user != null) {
Log.d(KAKAO_USER_INFO_LOG_TAG, KAKAO_USER_INFO_SUCCESS_MESSAGE)
Comment on lines +64 to +67
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

필요하지 않은 로그라면 삭제해볼까요?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

만일 카카오톡에서 사용자 정보를 불러오지 못했을 때 발생할 수 있는 에러에 대해 개발자가 상황을 확인하고 처리할 수 있도록 해당 로그를 넣었습니다.
훗날 파이어베이스 로그를 찍는 로직이 적용된다면 그 땐 지금 위의 로그 로직을 삭제해도 될 것 같아요!

_nickname.value = user.kakaoAccount?.profile?.nickname.toString()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

위와 마찬가지로 뷰모델 안에서 카카오에 대한 의존성이 발생하는 것 같습니다.
저도 당장 무엇이 가장 좋을지는 잘 모르겠지만
닉네임도 로컬 저장소에 저장했다가 가져오는 방법,
뷰모델이 아닌 액티비티에서 받는 방법 등 여러 방법들이 있을 것 같습니다.

}
}
}

private fun setErrorMessage(throwable: Throwable) {
when (throwable) {
is NaagaThrowable.ServerConnectFailure ->
_errorMessage.value = throwable.message
}
}

companion object {
private const val KAKAO_USER_INFO_LOG_TAG = "kakao user"
private const val KAKAO_USER_INFO_FAIL_MESSAGE = "사용자 정보 요청 실패"
private const val KAKAO_USER_INFO_SUCCESS_MESSAGE = "사용자 정보 요청 성공"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import com.now.domain.model.AdventureStatus.IN_PROGRESS
import com.now.domain.model.AdventureStatus.NONE
import com.now.naaga.R
import com.now.naaga.presentation.beginadventure.BeginAdventureActivity
import com.now.naaga.presentation.login.LoginActivity
import com.now.naaga.presentation.onadventure.OnAdventureActivity

class SplashActivity : AppCompatActivity() {
Expand Down Expand Up @@ -44,8 +45,8 @@ class SplashActivity : AppCompatActivity() {
OnAdventureActivity.getIntentWithAdventure(this, adventure)
}
}
DONE -> BeginAdventureActivity.getIntent(this)
NONE -> BeginAdventureActivity.getIntent(this)
DONE -> LoginActivity.getIntent(this)
NONE -> LoginActivity.getIntent(this)
}
startActivity(intent)
finish()
Expand Down
Loading