-
Notifications
You must be signed in to change notification settings - Fork 0
feat: Apple 로그인 기능 추가 (iOS) #66
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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
7 changes: 7 additions & 0 deletions
7
...p/src/androidMain/kotlin/com/chukchukhaksa/mobile/common/kmp/AppleSignInClient.android.kt
This file contains hidden or 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,7 @@ | ||
| package com.chukchukhaksa.mobile.common.kmp | ||
|
|
||
| actual class AppleSignInClient actual constructor() { | ||
| actual suspend fun signIn(): AppleSignInResult { | ||
| throw UnsupportedOperationException("Apple Sign In is not supported on Android") | ||
| } | ||
| } |
13 changes: 13 additions & 0 deletions
13
composeApp/src/commonMain/kotlin/com/chukchukhaksa/mobile/common/kmp/AppleSignInClient.kt
This file contains hidden or 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,13 @@ | ||
| package com.chukchukhaksa.mobile.common.kmp | ||
|
|
||
| data class AppleSignInResult( | ||
| val identityToken: String, | ||
| val authorizationCode: String, | ||
| val userId: String, | ||
| val email: String?, | ||
| val fullName: String?, | ||
| ) | ||
|
|
||
| expect class AppleSignInClient() { | ||
| suspend fun signIn(): AppleSignInResult | ||
| } |
This file contains hidden or 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
13 changes: 13 additions & 0 deletions
13
...p/src/commonMain/kotlin/com/chukchukhaksa/mobile/domain/auth/usecase/AppleLoginUseCase.kt
This file contains hidden or 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,13 @@ | ||
| package com.chukchukhaksa.mobile.domain.auth.usecase | ||
|
|
||
| import com.chukchukhaksa.mobile.common.kmp.AppleSignInResult | ||
| import com.chukchukhaksa.mobile.common.kmp.AppleSignInClient | ||
| import com.chukchukhaksa.mobile.domain.common.runCatchingIgnoreCancelled | ||
|
|
||
| class AppleLoginUseCase( | ||
| private val appleSignInClient: AppleSignInClient, | ||
| ) { | ||
| suspend operator fun invoke(): Result<AppleSignInResult> = runCatchingIgnoreCancelled { | ||
| appleSignInClient.signIn() | ||
| } | ||
| } |
This file contains hidden or 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
159 changes: 159 additions & 0 deletions
159
composeApp/src/iosMain/kotlin/com/chukchukhaksa/mobile/common/kmp/AppleSignInClient.ios.kt
This file contains hidden or 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,159 @@ | ||
| package com.chukchukhaksa.mobile.common.kmp | ||
|
|
||
| import kotlinx.cinterop.BetaInteropApi | ||
| import kotlinx.cinterop.ExperimentalForeignApi | ||
| import kotlinx.coroutines.CancellableContinuation | ||
| import kotlinx.coroutines.suspendCancellableCoroutine | ||
| import platform.AuthenticationServices.ASAuthorization | ||
| import platform.AuthenticationServices.ASAuthorizationAppleIDCredential | ||
| import platform.AuthenticationServices.ASAuthorizationAppleIDProvider | ||
| import platform.AuthenticationServices.ASAuthorizationController | ||
| import platform.AuthenticationServices.ASAuthorizationControllerDelegateProtocol | ||
| import platform.AuthenticationServices.ASAuthorizationControllerPresentationContextProvidingProtocol | ||
| import platform.AuthenticationServices.ASAuthorizationScopeEmail | ||
| import platform.AuthenticationServices.ASAuthorizationScopeFullName | ||
| import platform.AuthenticationServices.ASPresentationAnchor | ||
| import platform.Foundation.NSError | ||
| import platform.Foundation.NSString | ||
| import platform.Foundation.NSUTF8StringEncoding | ||
| import platform.Foundation.create | ||
| import platform.UIKit.UIApplication | ||
| import platform.UIKit.UIWindow | ||
| import platform.UIKit.UIWindowScene | ||
| import platform.darwin.NSObject | ||
| import kotlin.coroutines.resume | ||
| import kotlin.coroutines.resumeWithException | ||
|
|
||
| actual class AppleSignInClient actual constructor() { | ||
|
|
||
| // Strong reference 유지 (delegate/controller가 ARC로 해제되지 않도록) | ||
| private var currentDelegate: AppleSignInDelegate? = null | ||
| private var currentController: ASAuthorizationController? = null | ||
|
|
||
| actual suspend fun signIn(): AppleSignInResult = suspendCancellableCoroutine { continuation -> | ||
| val delegate = AppleSignInDelegate(continuation) { | ||
| currentDelegate = null | ||
| currentController = null | ||
| } | ||
| currentDelegate = delegate | ||
|
|
||
| val provider = ASAuthorizationAppleIDProvider() | ||
| val request = provider.createRequest() | ||
| request.requestedScopes = listOf( | ||
| ASAuthorizationScopeEmail, | ||
| ASAuthorizationScopeFullName, | ||
| ) | ||
|
|
||
| val controller = ASAuthorizationController( | ||
| authorizationRequests = listOf(request), | ||
| ) | ||
| controller.delegate = delegate | ||
| controller.presentationContextProvider = delegate | ||
| currentController = controller | ||
|
|
||
| continuation.invokeOnCancellation { | ||
| currentDelegate = null | ||
| currentController = null | ||
| } | ||
|
|
||
| controller.performRequests() | ||
| } | ||
| } | ||
|
|
||
| private class AppleSignInDelegate( | ||
| private val continuation: CancellableContinuation<AppleSignInResult>, | ||
| private val onComplete: () -> Unit, | ||
| ) : NSObject(), | ||
| ASAuthorizationControllerDelegateProtocol, | ||
| ASAuthorizationControllerPresentationContextProvidingProtocol { | ||
|
|
||
| @OptIn(BetaInteropApi::class) | ||
| override fun authorizationController( | ||
| controller: ASAuthorizationController, | ||
| didCompleteWithAuthorization: ASAuthorization, | ||
| ) { | ||
| val credential = didCompleteWithAuthorization.credential as? ASAuthorizationAppleIDCredential | ||
| ?: run { | ||
| onComplete() | ||
| continuation.resumeWithException( | ||
| IllegalStateException("Invalid credential type"), | ||
| ) | ||
| return | ||
| } | ||
|
|
||
| val identityTokenData = credential.identityToken | ||
| val authorizationCodeData = credential.authorizationCode | ||
|
|
||
| if (identityTokenData == null || authorizationCodeData == null) { | ||
| onComplete() | ||
| continuation.resumeWithException( | ||
| IllegalStateException("Missing identityToken or authorizationCode"), | ||
| ) | ||
| return | ||
| } | ||
|
|
||
| val identityToken = NSString.create( | ||
| data = identityTokenData, | ||
| encoding = NSUTF8StringEncoding, | ||
| )?.toString() ?: run { | ||
| onComplete() | ||
| continuation.resumeWithException( | ||
| IllegalStateException("Failed to decode identityToken"), | ||
| ) | ||
| return | ||
| } | ||
|
|
||
| val authorizationCode = NSString.create( | ||
| data = authorizationCodeData, | ||
| encoding = NSUTF8StringEncoding, | ||
| )?.toString() ?: run { | ||
| onComplete() | ||
| continuation.resumeWithException( | ||
| IllegalStateException("Failed to decode authorizationCode"), | ||
| ) | ||
| return | ||
| } | ||
|
|
||
| val fullName = credential.fullName?.let { nameComponents -> | ||
| listOfNotNull( | ||
| nameComponents.familyName, | ||
| nameComponents.givenName, | ||
| ).joinToString(" ").ifBlank { null } | ||
| } | ||
|
|
||
| onComplete() | ||
| continuation.resume( | ||
| AppleSignInResult( | ||
| identityToken = identityToken, | ||
| authorizationCode = authorizationCode, | ||
| userId = credential.user, | ||
| email = credential.email, | ||
| fullName = fullName, | ||
| ), | ||
| ) | ||
| } | ||
|
|
||
| override fun authorizationController( | ||
| controller: ASAuthorizationController, | ||
| didCompleteWithError: NSError, | ||
| ) { | ||
| onComplete() | ||
| continuation.resumeWithException( | ||
| Exception("Apple Sign In failed: ${didCompleteWithError.localizedDescription}"), | ||
| ) | ||
| } | ||
|
|
||
| @OptIn(ExperimentalForeignApi::class) | ||
| override fun presentationAnchorForAuthorizationController( | ||
| controller: ASAuthorizationController, | ||
| ): ASPresentationAnchor { | ||
| val windowScene = UIApplication.sharedApplication.connectedScenes | ||
| .filterIsInstance<UIWindowScene>() | ||
| .firstOrNull() | ||
|
|
||
| return windowScene?.windows | ||
| ?.filterIsInstance<UIWindow>() | ||
| ?.firstOrNull { it.isKeyWindow() } | ||
| ?: UIWindow() | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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,10 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> | ||
| <plist version="1.0"> | ||
| <dict> | ||
| <key>com.apple.developer.applesignin</key> | ||
| <array> | ||
| <string>Default</string> | ||
| </array> | ||
| </dict> | ||
| </plist> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
string 값 하드코딩된 상태여도 괜찮을까요??
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
요거 디자인 나오기 전이라서 임시로 넣어놓은거라 괜찮을거같아유