5
5
// Created by Guilherme Souza on 16/12/23.
6
6
//
7
7
8
+ import Auth
8
9
import AuthenticationServices
9
10
import SwiftUI
10
11
@@ -14,7 +15,7 @@ struct SignInWithApple: View {
14
15
var body : some View {
15
16
VStack {
16
17
SignInWithAppleButton { request in
17
- request. requestedScopes = [ . email]
18
+ request. requestedScopes = [ . email, . fullName ]
18
19
} onCompletion: { result in
19
20
switch result {
20
21
case let . failure( error) :
@@ -29,16 +30,23 @@ struct SignInWithApple: View {
29
30
return
30
31
}
31
32
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 {
36
41
debug ( " Invalid identity token " )
37
42
return
38
43
}
39
44
40
45
Task {
41
- await signInWithApple ( using: identityToken)
46
+ await signInWithApple (
47
+ using: identityToken,
48
+ fullName: credential. fullName? . formatted ( )
49
+ )
42
50
}
43
51
}
44
52
}
@@ -55,13 +63,21 @@ struct SignInWithApple: View {
55
63
}
56
64
}
57
65
58
- private func signInWithApple( using idToken: String ) async {
66
+ private func signInWithApple( using idToken: String , fullName : String ? ) async {
59
67
actionState = . inFlight
60
68
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
+ }
65
81
}
66
82
actionState = . result( result)
67
83
}
0 commit comments