Skip to content

Commit

Permalink
[FEAT]#7: Kakao login 기능 추
Browse files Browse the repository at this point in the history
  • Loading branch information
flash159483 committed Jul 15, 2024
1 parent 13306e3 commit 50bad3b
Show file tree
Hide file tree
Showing 5 changed files with 123 additions and 6 deletions.
5 changes: 1 addition & 4 deletions app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ android {

defaultConfig {
buildConfigField("String", "KAKAO_APP_KEY", properties.getProperty("KAKAO_APP_KEY"))
resValue(
"string",
"SCHEME_KAKAO_APP_KEY",
manifestPlaceholders["SCHEME_KAKAO_APP_KEY"] =
properties.getProperty("SCHEME_KAKAO_APP_KEY")
)
}

buildFeatures {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@

<data
android:host="oauth"
android:scheme="@string/SCHEME_KAKAO_APP_KEY" />
android:scheme="${SCHEME_KAKAO_APP_KEY}" />
</intent-filter>
</activity>
</application>
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/kotlin/com/bff/wespot/WeSpotApplication.kt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import com.kakao.sdk.common.KakaoSdk
class WeSpotApplication : Application() {
override fun onCreate() {
super.onCreate()
KakaoSdk.init(this, BuildConfig.KAKAO_APP_KEY)

val key = BuildConfig.KAKAO_APP_KEY
KakaoSdk.init(this, key)
}
}
37 changes: 37 additions & 0 deletions feature/auth/src/main/kotlin/com/bff/wespot/auth/AuthScreen.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,45 @@
package com.bff.wespot.auth

import android.util.Log
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.rememberCoroutineScope
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.platform.LocalContext
import com.kakao.sdk.common.model.ClientError
import com.kakao.sdk.common.model.ClientErrorCause
import kotlinx.coroutines.launch

@Composable
fun AuthScreen() {
val context = LocalContext.current
val coroutineScope = rememberCoroutineScope()
val kakaoLoginManager = KakaoLoginManager(context)


Box(
modifier = Modifier.fillMaxSize(),
contentAlignment = Alignment.Center
) {
Button(onClick = {
coroutineScope.launch {
try {
val token = kakaoLoginManager.loginWithKakao()
Log.d("AUTHSCREEN", token.toString())
} catch (e: Exception) {
if (e is ClientError && e.reason == ClientErrorCause.Cancelled) {
Log.d("AUTHSCREEN", "Login cancelled")
} else {
Log.e("AUTHSCREEN", "Login failed", e)
}
}
}
}) {
Text(text = "Login with Kakao")
}
}
}
81 changes: 81 additions & 0 deletions feature/auth/src/main/kotlin/com/bff/wespot/auth/KakaoLogin.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package com.bff.wespot.auth

import android.content.Context
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.user.UserApiClient
import dagger.hilt.android.qualifiers.ActivityContext
import javax.inject.Inject
import kotlin.coroutines.Continuation
import kotlin.coroutines.resume
import kotlin.coroutines.resumeWithException
import kotlin.coroutines.suspendCoroutine

class KakaoLoginManager @Inject constructor(
@ActivityContext private val context: Context
) {

suspend fun loginWithKakao(): OAuthToken {
val loginState = getKakaoLoginState()

return when (loginState) {
KaKaoLoginState.KAKAO_TALK_LOGIN -> {
try {
UserApiClient.loginWithKakaoTalk()
} catch (error: Throwable) {
if (error is ClientError && error.reason == ClientErrorCause.Cancelled) {
throw error
}

UserApiClient.loginWithKakaoAccount()
}
}

KaKaoLoginState.KAKAO_ACCOUNT_LOGIN -> {
UserApiClient.loginWithKakaoAccount()
}
}
}


private suspend fun UserApiClient.Companion.loginWithKakaoTalk(): OAuthToken {
return suspendCoroutine { continuation ->
instance.loginWithKakaoTalk(context) { token, error ->
continuation.resumeTokenOrException(token, error)
}
}
}

private suspend fun UserApiClient.Companion.loginWithKakaoAccount(): OAuthToken {
return suspendCoroutine { continuation ->
instance.loginWithKakaoAccount(context) { token, error ->
continuation.resumeTokenOrException(token, error)
}
}
}

private fun getKakaoLoginState(): KaKaoLoginState =
if (UserApiClient.instance.isKakaoTalkLoginAvailable(context)) {
KaKaoLoginState.KAKAO_TALK_LOGIN
} else {
KaKaoLoginState.KAKAO_ACCOUNT_LOGIN
}

private fun Continuation<OAuthToken>.resumeTokenOrException(
token: OAuthToken?,
error: Throwable?
) {
if (error != null) {
resumeWithException(error)
} else if (token != null) {
resume(token)
} else {
resumeWithException(RuntimeException("Failed to get kakao access token, reason is not clear."))
}
}
}

enum class KaKaoLoginState {
KAKAO_TALK_LOGIN, KAKAO_ACCOUNT_LOGIN
}

0 comments on commit 50bad3b

Please sign in to comment.