Skip to content

Commit 10c1d3e

Browse files
Merge pull request #9 from Compiler-Inc/fix/auth-problems
fix: remove some apple auth stuff
2 parents 2a05955 + d484b9e commit 10c1d3e

File tree

2 files changed

+23
-10
lines changed

2 files changed

+23
-10
lines changed

Sources/CompilerSwiftAI/Auth/CompilerCient+AppleAuth.swift

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
import AuthenticationServices
44

55
public extension CompilerClient {
6-
func handleSignInWithApple(_ result: Result<ASAuthorization, Error>, nonce: String?) async throws -> Bool {
6+
func handleSignInWithApple(_ result: Result<ASAuthorization, Error>, nonce: String?) async throws {
77
switch result {
88
case let .success(auth):
99
guard let appleIDCredential = auth.credential as? ASAuthorizationAppleIDCredential,
@@ -20,12 +20,21 @@ public extension CompilerClient {
2020
if let nonce = nonce {
2121
await keychain.save(nonce, service: "apple-nonce", account: "user")
2222
}
23+
24+
let userIdentifier = appleIDCredential.user
25+
let fullName = appleIDCredential.fullName
26+
let email = appleIDCredential.email
27+
28+
let formatter = PersonNameComponentsFormatter()
2329

24-
let accessToken = try await authenticateWithServer(idToken: idToken, nonce: nonce)
30+
let accessToken = try await authenticateWithServer(
31+
idToken: idToken,
32+
nonce: nonce,
33+
email: email,
34+
name: fullName.map { formatter.string(from: $0) }
35+
)
36+
2537
await keychain.save(accessToken, service: "access-token", account: "user")
26-
27-
return true
28-
2938
case let .failure(error):
3039
throw AuthError.networkError(error)
3140
}

Sources/CompilerSwiftAI/Auth/CompilerClient+Auth.swift

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,13 @@ public enum AuthError: Error {
1111
}
1212

1313
extension CompilerClient {
14+
struct AppleSignInRequest: Encodable {
15+
let id_token: String
16+
let nonce: String?
17+
let email: String?
18+
let name: String?
19+
}
20+
1421
/// Gets either the stored Apple ID Token or a refreshed one
1522
/// - Returns: Token string
1623
func getValidToken() async throws -> String {
@@ -28,7 +35,7 @@ extension CompilerClient {
2835
throw AuthError.invalidToken
2936
}
3037

31-
func authenticateWithServer(idToken: String, nonce: String? = nil) async throws -> String {
38+
func authenticateWithServer(idToken: String, nonce: String? = nil, email: String? = nil, name: String? = nil) async throws -> String {
3239
let lowercasedAppID = appID.lowercased()
3340
let endpoint = "\(baseURL)/v1/apps/\(lowercasedAppID)/end-users/apple"
3441

@@ -37,10 +44,7 @@ extension CompilerClient {
3744
throw AuthError.invalidResponse
3845
}
3946

40-
var body: [String: String] = ["id_token": idToken]
41-
if let nonce {
42-
body["nonce"] = nonce
43-
}
47+
let body = AppleSignInRequest(id_token: idToken, nonce: nonce, email: email, name: name)
4448
authLogger.debug("Making auth request to: \(endpoint)")
4549

4650
var request = URLRequest(url: url)

0 commit comments

Comments
 (0)