Skip to content
Merged
Changes from all commits
Commits
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
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import com.kakao.sdk.auth.model.OAuthToken
import com.kakao.sdk.common.model.ApiError
import com.kakao.sdk.common.model.AuthError
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 kotlinx.coroutines.CancellableContinuation
Expand All @@ -28,22 +29,20 @@ class KakaoLoginManager(
) {
unlinkKakao()
try {
val accessToken =
if (UserApiClient.instance.isKakaoTalkLoginAvailable(context)) {
loginWithKakaoTalk()
} else {
loginWithKakaoAccount()
}
val accessToken = fetchAccessToken()
onSuccess(accessToken)
} catch (error: ClientError) {
if (error.reason == ClientErrorCause.Cancelled) {
onFailure(Exception(context.getString(R.string.login_user_cancelled)))
} else {
onFailure(Exception(context.getString(R.string.login_client_error)))
}
} catch (error: AuthError) {
// OAuth 인증 과정 에러
onFailure(Exception(context.getString(R.string.login_auth_error)))
} catch (error: ApiError) {
// API 호출 에러
onFailure(Exception(context.getString(R.string.login_api_error)))
} catch (error: ClientError) {
// SDK 내부 에러
onFailure(Exception(context.getString(R.string.login_client_error)))
} catch (error: KakaoSdkError) {
// 카카오 SDK 에러
onFailure(Exception(context.getString(R.string.login_sdk_error)))
Expand Down Expand Up @@ -84,28 +83,35 @@ class KakaoLoginManager(
}
}

private suspend fun fetchAccessToken(): String {
if (UserApiClient.instance.isKakaoTalkLoginAvailable(context).not()) {
return loginWithKakaoAccount()
}
return runCatching { loginWithKakaoTalk() }
.getOrElse {
// 카카오톡 로그인 단계에서 실패하면 즉시 웹 계정 로그인으로 폴백
loginWithKakaoAccount()
}
}

// 카카오 로그인 결과 처리
private fun handleLoginResult(
token: OAuthToken?,
error: Throwable?,
continuation: CancellableContinuation<String>,
) {
when {
error != null -> {
token != null -> continuation.resume(token.accessToken)
error != null ->
continuation.resumeWith(
Result.failure(
Exception(context.getString(R.string.login_user_cancelled)),
),
Result.failure(error),
)
}
token != null -> continuation.resume(token.accessToken)
else -> {
else ->
continuation.resumeWith(
Result.failure(
Exception(context.getString(R.string.login_user_cancelled)),
),
)
Comment on lines +109 to 114

Choose a reason for hiding this comment

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

medium

handleLoginResult 함수의 when 표현식에서 tokenerror가 모두 nullelse 분기는 카카오 SDK 명세상 발생해서는 안 되는 경우입니다. 현재 코드는 이 예외적인 상황을 사용자가 로그인을 취소한 것으로 간주하고 있는데, 이는 잠재적인 SDK 문제를 숨기고 디버깅을 어렵게 만들 수 있습니다.

이러한 예기치 않은 상태는 IllegalStateException과 같이 더 명확한 예외를 발생시켜 처리하는 것이 좋습니다. 이를 통해 개발 중에 문제를 더 빨리 파악하고 대응할 수 있습니다.

Suggested change
else ->
continuation.resumeWith(
Result.failure(
Exception(context.getString(R.string.login_user_cancelled)),
),
)
else ->
continuation.resumeWith(
Result.failure(
IllegalStateException("Kakao login returned null for both token and error"),
),
)

}
}
}
}