S
Supabase7mo ago
Alex

[Swift] Sign In With Apple, anon users, & linkIdentity

Hi team! Lets say I create an iOS app where the default user is signed in anonymously on launch via:
let session = try await supabase.auth.signInAnonymously(
data: [ "appleIdentifierForVendor": .string(appleDeviceID)])
let session = try await supabase.auth.signInAnonymously(
data: [ "appleIdentifierForVendor": .string(appleDeviceID)])
At some point later, the user wants to make an account. How do I present them a SignInWithApple auth in SwiftUI? The try await client.auth.linkIdentity(provider: .apple) doesn't seem to take an Auth token for the payload / seems to try to direct to a URL so I'm not sure how I'd make use of it.
do {
guard
let credential = try result.get().credential
as? ASAuthorizationAppleIDCredential
else {
logger.error("Error signing in with Apple: No credential")
return
}

guard
let idToken = credential.identityToken
.flatMap({ String(data: $0, encoding: .utf8) })
else {
logger.error("Error signing in with Apple: No idToken")
return
}

//Convert from anonymous user, but this tries to open a URL or something instead of using the auth token?
try await client.auth.linkIdentity(provider: .apple)


// This is how we'd sign in normally if there wasn't an anonymous user we wanted to link
// try await client.auth.signInWithIdToken(
// credentials: .init(
// provider: .apple,
// idToken: idToken
// )
// )
do {
guard
let credential = try result.get().credential
as? ASAuthorizationAppleIDCredential
else {
logger.error("Error signing in with Apple: No credential")
return
}

guard
let idToken = credential.identityToken
.flatMap({ String(data: $0, encoding: .utf8) })
else {
logger.error("Error signing in with Apple: No idToken")
return
}

//Convert from anonymous user, but this tries to open a URL or something instead of using the auth token?
try await client.auth.linkIdentity(provider: .apple)


// This is how we'd sign in normally if there wasn't an anonymous user we wanted to link
// try await client.auth.signInWithIdToken(
// credentials: .init(
// provider: .apple,
// idToken: idToken
// )
// )
Could someone assist? Thank you so much
1 Reply
Guilherme Souza
Guilherme Souza7mo ago
Hi, we currently don't support linking identities using the native flow, i.e. signInWithIdToken, you can have more info on https://github.com/supabase/supabase-swift/issues/588 The way it currently works is to call the linkidentity while having a anonymouys session, it will navigate to a browser where you'll sign in with your apple account, which will link it to the anonymous session
GitHub
Link apple identity natively · Issue #588 · supabase/supabase-swift
Feature Request We have native apple sign in, but no way (that I can see) of natively linking Apple as a sign in option to an existing account, we need to use the OAuth flow which redirects to web ...

Did you find this page helpful?