Skip to content

Commit

Permalink
docs(examples): update sign in with apple example to save user's full…
Browse files Browse the repository at this point in the history
… name
  • Loading branch information
grdsdev committed Dec 17, 2024
1 parent bbff268 commit 6140a34
Showing 1 changed file with 27 additions and 11 deletions.
38 changes: 27 additions & 11 deletions Examples/Examples/Auth/SignInWithApple.swift
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// Created by Guilherme Souza on 16/12/23.
//

import Auth
import AuthenticationServices
import SwiftUI

Expand All @@ -14,7 +15,7 @@ struct SignInWithApple: View {
var body: some View {
VStack {
SignInWithAppleButton { request in
request.requestedScopes = [.email]
request.requestedScopes = [.email, .fullName]
} onCompletion: { result in
switch result {
case let .failure(error):
Expand All @@ -29,16 +30,23 @@ struct SignInWithApple: View {
return
}

guard let identityToken = credential.identityToken.flatMap({ String(
data: $0,
encoding: .utf8
) }) else {
guard
let identityToken = credential.identityToken.flatMap({
String(
data: $0,
encoding: .utf8
)
})
else {
debug("Invalid identity token")
return
}

Task {
await signInWithApple(using: identityToken)
await signInWithApple(
using: identityToken,
fullName: credential.fullName?.formatted()
)
}
}
}
Expand All @@ -55,13 +63,21 @@ struct SignInWithApple: View {
}
}

private func signInWithApple(using idToken: String) async {
private func signInWithApple(using idToken: String, fullName: String?) async {
actionState = .inFlight
let result = await Result {
_ = try await supabase.auth.signInWithIdToken(credentials: .init(
provider: .apple,
idToken: idToken
))
_ = try await supabase.auth.signInWithIdToken(
credentials: .init(
provider: .apple,
idToken: idToken
))

// fullName is provided only in the first time (account creation),
// so checking if it is non-nil to not erase data on login.
if let fullName {
_ = try? await supabase.auth.update(
user: UserAttributes(data: ["full_name": .string(fullName)]))
}
}
actionState = .result(result)
}
Expand Down

0 comments on commit 6140a34

Please sign in to comment.