Skip to content

Commit da36f30

Browse files
committed
add link identity with oidc example
1 parent c4cfc4b commit da36f30

File tree

4 files changed

+71
-6
lines changed

4 files changed

+71
-6
lines changed

Examples/Examples.xcodeproj/project.pbxproj

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -807,7 +807,6 @@
807807
SUPPORTS_MACCATALYST = NO;
808808
SWIFT_ACTIVE_COMPILATION_CONDITIONS = "DEBUG $(inherited)";
809809
SWIFT_EMIT_LOC_STRINGS = YES;
810-
SWIFT_VERSION = 5.0;
811810
TARGETED_DEVICE_FAMILY = "1,2,7";
812811
};
813812
name = Debug;
@@ -847,7 +846,6 @@
847846
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator macosx xros xrsimulator";
848847
SUPPORTS_MACCATALYST = NO;
849848
SWIFT_EMIT_LOC_STRINGS = YES;
850-
SWIFT_VERSION = 5.0;
851849
TARGETED_DEVICE_FAMILY = "1,2,7";
852850
};
853851
name = Release;
@@ -888,7 +886,6 @@
888886
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator macosx";
889887
SWIFT_ACTIVE_COMPILATION_CONDITIONS = "DEBUG $(inherited)";
890888
SWIFT_EMIT_LOC_STRINGS = YES;
891-
SWIFT_VERSION = 5.0;
892889
TARGETED_DEVICE_FAMILY = "1,2";
893890
};
894891
name = Debug;
@@ -928,7 +925,6 @@
928925
SDKROOT = auto;
929926
SUPPORTED_PLATFORMS = "iphoneos iphonesimulator macosx";
930927
SWIFT_EMIT_LOC_STRINGS = YES;
931-
SWIFT_VERSION = 5.0;
932928
TARGETED_DEVICE_FAMILY = "1,2";
933929
};
934930
name = Release;

Examples/Examples/Examples.entitlements

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
33
<plist version="1.0">
44
<dict>
5+
<key>com.apple.developer.applesignin</key>
6+
<array>
7+
<string>Default</string>
8+
</array>
59
<key>com.apple.security.app-sandbox</key>
610
<true/>
711
<key>com.apple.security.files.user-selected.read-only</key>

Examples/Examples/Profile/UserIdentityList.swift

Lines changed: 65 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// Created by Guilherme Souza on 22/03/24.
66
//
77

8+
import AuthenticationServices
89
import Supabase
910
import SwiftUI
1011

@@ -62,7 +63,11 @@ struct UserIdentityList: View {
6263
Button(provider.rawValue) {
6364
Task {
6465
do {
65-
try await supabase.auth.linkIdentity(provider: provider)
66+
if provider == .apple {
67+
try await linkAppleIdentity()
68+
} else {
69+
try await supabase.auth.linkIdentity(provider: provider)
70+
}
6671
} catch {
6772
self.error = error
6873
}
@@ -74,8 +79,67 @@ struct UserIdentityList: View {
7479
}
7580
#endif
7681
}
82+
83+
private func linkAppleIdentity() async throws {
84+
let provider = ASAuthorizationAppleIDProvider()
85+
let request = provider.createRequest()
86+
request.requestedScopes = [.email, .fullName]
87+
88+
let controller = ASAuthorizationController(authorizationRequests: [request])
89+
let authorization = try await controller.performRequests()
90+
91+
guard let credential = authorization.credential as? ASAuthorizationAppleIDCredential else {
92+
debug("Invalid credential")
93+
return
94+
}
95+
96+
guard
97+
let identityToken = credential.identityToken.flatMap({ String(data: $0, encoding: .utf8) })
98+
else {
99+
debug("Invalid identity token")
100+
return
101+
}
102+
103+
try await supabase.auth.linkIdentityWithIdToken(
104+
credentials: OpenIDConnectCredentials(
105+
provider: .apple,
106+
idToken: identityToken
107+
)
108+
)
109+
}
77110
}
78111

79112
#Preview {
80113
UserIdentityList()
81114
}
115+
116+
extension ASAuthorizationController {
117+
@MainActor
118+
func performRequests() async throws -> ASAuthorization {
119+
let delegate = _Delegate()
120+
self.delegate = delegate
121+
return try await withCheckedThrowingContinuation { continuation in
122+
delegate.continuation = continuation
123+
124+
self.performRequests()
125+
}
126+
}
127+
128+
private final class _Delegate: NSObject, ASAuthorizationControllerDelegate {
129+
var continuation: CheckedContinuation<ASAuthorization, any Error>?
130+
131+
func authorizationController(
132+
controller: ASAuthorizationController,
133+
didCompleteWithAuthorization authorization: ASAuthorization
134+
) {
135+
continuation?.resume(returning: authorization)
136+
}
137+
138+
func authorizationController(
139+
controller: ASAuthorizationController,
140+
didCompleteWithError error: any Error
141+
) {
142+
continuation?.resume(throwing: error)
143+
}
144+
}
145+
}

Supabase.xcworkspace/xcshareddata/swiftpm/Package.resolved

Lines changed: 2 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)