Created
May 10, 2025 19:29
-
-
Save blakelee/bcda531e2c4bb585b6fd4fe3271d2fa3 to your computer and use it in GitHub Desktop.
iOS Apple Login Kotlin
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
| class IosAppleAuthentication( | |
| private val supabase: SupabaseClient | |
| ) : AppleAuthentication { | |
| override suspend fun login() { | |
| val token = getToken() | |
| supabase.auth.signInWith(IDToken) { | |
| provider = Apple | |
| idToken = token | |
| } | |
| } | |
| private suspend fun getToken(): String = suspendCancellableCoroutine { continuation -> | |
| val request = ASAuthorizationAppleIDProvider().createRequest() | |
| request.requestedScopes = listOf(ASAuthorizationScopeFullName, ASAuthorizationScopeEmail) | |
| val controller = ASAuthorizationController(arrayListOf(request)) | |
| val delegate = object : NSObject(), ASAuthorizationControllerDelegateProtocol { | |
| override fun authorizationController( | |
| controller: ASAuthorizationController, | |
| didCompleteWithAuthorization: ASAuthorization | |
| ) { | |
| val credential = | |
| didCompleteWithAuthorization.credential as? ASAuthorizationAppleIDCredential | |
| if (credential != null) { | |
| val token = credential.identityToken!!.string() | |
| continuation.resume(token) | |
| } else { | |
| continuation.resumeWithException(Exception("Invalid credential")) | |
| } | |
| } | |
| override fun authorizationController( | |
| controller: ASAuthorizationController, | |
| didCompleteWithError: NSError | |
| ) { | |
| continuation.resumeWithException(Exception(didCompleteWithError.localizedDescription)) | |
| } | |
| } | |
| controller.delegate = delegate | |
| controller.performRequests() | |
| } | |
| override suspend fun logout() { | |
| } | |
| fun NSData.string(): String { | |
| return NSString.create(data = this, encoding = NSUTF8StringEncoding).toString() | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment