-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
13306e3
commit 50bad3b
Showing
5 changed files
with
123 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
feature/auth/src/main/kotlin/com/bff/wespot/auth/AuthScreen.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
81
feature/auth/src/main/kotlin/com/bff/wespot/auth/KakaoLogin.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |