-
-
Notifications
You must be signed in to change notification settings - Fork 109
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(auth): update user after anonymous sign in (#433)
- Loading branch information
Showing
4 changed files
with
141 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
// | ||
// UpdateProfileView.swift | ||
// Examples | ||
// | ||
// Created by Guilherme Souza on 14/05/24. | ||
// | ||
|
||
import SwiftUI | ||
import Supabase | ||
|
||
struct UpdateProfileView: View { | ||
let user: User | ||
|
||
@State var email = "" | ||
@State var phone = "" | ||
|
||
@State var otp = "" | ||
@State var showTokenField = false | ||
|
||
var formUpdated: Bool { | ||
emailChanged || phoneChanged | ||
} | ||
|
||
var emailChanged: Bool { | ||
email != user.email | ||
} | ||
|
||
var phoneChanged: Bool { | ||
phone != user.phone | ||
} | ||
|
||
var body: some View { | ||
Form { | ||
Section { | ||
TextField("Email", text: $email) | ||
.textContentType(.emailAddress) | ||
.keyboardType(.emailAddress) | ||
.autocorrectionDisabled() | ||
.textInputAutocapitalization(.never) | ||
TextField("Phone", text: $phone) | ||
.textContentType(.telephoneNumber) | ||
.keyboardType(.phonePad) | ||
.autocorrectionDisabled() | ||
.textInputAutocapitalization(.never) | ||
} | ||
|
||
Section { | ||
Button("Update") { | ||
Task { | ||
await updateButtonTapped() | ||
} | ||
} | ||
.disabled(!formUpdated) | ||
} | ||
|
||
if showTokenField { | ||
Section { | ||
TextField("OTP", text: $otp) | ||
Button("Verify") { | ||
Task { | ||
await verifyTapped() | ||
} | ||
} | ||
} | ||
} | ||
} | ||
.onAppear { | ||
email = user.email ?? "" | ||
phone = user.phone ?? "" | ||
} | ||
} | ||
|
||
@MainActor | ||
private func updateButtonTapped() async { | ||
var attributes = UserAttributes() | ||
if emailChanged { | ||
attributes.email = email | ||
} | ||
|
||
if phoneChanged { | ||
attributes.phone = phone | ||
} | ||
|
||
do { | ||
try await supabase.auth.update(user: attributes, redirectTo: Constants.redirectToURL) | ||
|
||
if phoneChanged { | ||
showTokenField = true | ||
} | ||
} catch { | ||
debug("Fail to update user: \(error)") | ||
} | ||
} | ||
|
||
@MainActor | ||
private func verifyTapped() async { | ||
do { | ||
try await supabase.auth.verifyOTP(phone: phone, token: otp, type: .phoneChange) | ||
} catch { | ||
debug("Fail to verify OTP: \(error)") | ||
} | ||
} | ||
} | ||
|
||
#Preview { | ||
UpdateProfileView( | ||
user: User( | ||
id: UUID(), | ||
appMetadata: [:], | ||
userMetadata: [:], | ||
aud: "", | ||
createdAt: Date(), | ||
updatedAt: Date() | ||
) | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters