Skip to content

Commit 4d33c9c

Browse files
authored
docs(examples): update sign in with apple example to save user's full name (#628)
1 parent bbff268 commit 4d33c9c

File tree

1 file changed

+27
-11
lines changed

1 file changed

+27
-11
lines changed

Examples/Examples/Auth/SignInWithApple.swift

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// Created by Guilherme Souza on 16/12/23.
66
//
77

8+
import Auth
89
import AuthenticationServices
910
import SwiftUI
1011

@@ -14,7 +15,7 @@ struct SignInWithApple: View {
1415
var body: some View {
1516
VStack {
1617
SignInWithAppleButton { request in
17-
request.requestedScopes = [.email]
18+
request.requestedScopes = [.email, .fullName]
1819
} onCompletion: { result in
1920
switch result {
2021
case let .failure(error):
@@ -29,16 +30,23 @@ struct SignInWithApple: View {
2930
return
3031
}
3132

32-
guard let identityToken = credential.identityToken.flatMap({ String(
33-
data: $0,
34-
encoding: .utf8
35-
) }) else {
33+
guard
34+
let identityToken = credential.identityToken.flatMap({
35+
String(
36+
data: $0,
37+
encoding: .utf8
38+
)
39+
})
40+
else {
3641
debug("Invalid identity token")
3742
return
3843
}
3944

4045
Task {
41-
await signInWithApple(using: identityToken)
46+
await signInWithApple(
47+
using: identityToken,
48+
fullName: credential.fullName?.formatted()
49+
)
4250
}
4351
}
4452
}
@@ -55,13 +63,21 @@ struct SignInWithApple: View {
5563
}
5664
}
5765

58-
private func signInWithApple(using idToken: String) async {
66+
private func signInWithApple(using idToken: String, fullName: String?) async {
5967
actionState = .inFlight
6068
let result = await Result {
61-
_ = try await supabase.auth.signInWithIdToken(credentials: .init(
62-
provider: .apple,
63-
idToken: idToken
64-
))
69+
_ = try await supabase.auth.signInWithIdToken(
70+
credentials: .init(
71+
provider: .apple,
72+
idToken: idToken
73+
))
74+
75+
// fullName is provided only in the first time (account creation),
76+
// so checking if it is non-nil to not erase data on login.
77+
if let fullName {
78+
_ = try? await supabase.auth.update(
79+
user: UserAttributes(data: ["full_name": .string(fullName)]))
80+
}
6581
}
6682
actionState = .result(result)
6783
}

0 commit comments

Comments
 (0)