Skip to content

Instantly share code, notes, and snippets.

@blakelee
Created May 10, 2025 19:29
Show Gist options
  • Select an option

  • Save blakelee/bcda531e2c4bb585b6fd4fe3271d2fa3 to your computer and use it in GitHub Desktop.

Select an option

Save blakelee/bcda531e2c4bb585b6fd4fe3271d2fa3 to your computer and use it in GitHub Desktop.
iOS Apple Login Kotlin
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