Skip to content

Commit

Permalink
test(auth): update user after anonymous sign in (#433)
Browse files Browse the repository at this point in the history
  • Loading branch information
grdsdev authored Jul 5, 2024
1 parent 7b0d661 commit 6f984ac
Show file tree
Hide file tree
Showing 4 changed files with 141 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Examples/Examples.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
79AF04862B2CE586008761AD /* Debug.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79AF04852B2CE586008761AD /* Debug.swift */; };
79B1C80C2BABFF8000D991AA /* ProfileView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79B1C80B2BABFF8000D991AA /* ProfileView.swift */; };
79B1C80E2BAC017C00D991AA /* AnyJSONView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79B1C80D2BAC017C00D991AA /* AnyJSONView.swift */; };
79B3261C2BF359A50023661C /* UpdateProfileView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79B3261B2BF359A50023661C /* UpdateProfileView.swift */; };
79B8F4242B5FED7C0000E839 /* IdentifiedCollections in Frameworks */ = {isa = PBXBuildFile; productRef = 79B8F4232B5FED7C0000E839 /* IdentifiedCollections */; };
79B8F4262B602F640000E839 /* Logger.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79B8F4252B602F640000E839 /* Logger.swift */; };
79BD76772B59C3E300CA3D68 /* UserStore.swift in Sources */ = {isa = PBXBuildFile; fileRef = 79BD76762B59C3E300CA3D68 /* UserStore.swift */; };
Expand Down Expand Up @@ -111,6 +112,7 @@
79AF04852B2CE586008761AD /* Debug.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Debug.swift; sourceTree = "<group>"; };
79B1C80B2BABFF8000D991AA /* ProfileView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ProfileView.swift; sourceTree = "<group>"; };
79B1C80D2BAC017C00D991AA /* AnyJSONView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AnyJSONView.swift; sourceTree = "<group>"; };
79B3261B2BF359A50023661C /* UpdateProfileView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UpdateProfileView.swift; sourceTree = "<group>"; };
79B8F4252B602F640000E839 /* Logger.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Logger.swift; sourceTree = "<group>"; };
79BD76762B59C3E300CA3D68 /* UserStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UserStore.swift; sourceTree = "<group>"; };
79BD76782B59C53900CA3D68 /* ChannelStore.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChannelStore.swift; sourceTree = "<group>"; };
Expand Down Expand Up @@ -273,6 +275,7 @@
children = (
79B1C80B2BABFF8000D991AA /* ProfileView.swift */,
794C61D52BAD1E12000E6B0F /* UserIdentityList.swift */,
79B3261B2BF359A50023661C /* UpdateProfileView.swift */,
);
path = Profile;
sourceTree = "<group>";
Expand Down Expand Up @@ -498,6 +501,7 @@
793895CC2954ABFF0044F2B8 /* RootView.swift in Sources */,
7956406A2955AFBD0088A06F /* ErrorText.swift in Sources */,
79AF04812B2CE261008761AD /* AuthView.swift in Sources */,
79B3261C2BF359A50023661C /* UpdateProfileView.swift in Sources */,
794EF1242955F3DE008C9526 /* TodoListRow.swift in Sources */,
797EFB662BABD82A00098D6B /* BucketList.swift in Sources */,
79E2B55C2B97A2310042CD21 /* UIApplicationExtensions.swift in Sources */,
Expand Down
12 changes: 10 additions & 2 deletions Examples/Examples/Profile/ProfileView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,17 @@ struct ProfileView: View {
var body: some View {
NavigationStack {
List {
if let user = user.flatMap({ try? AnyJSON($0) }) {
if let user,
let json = try? AnyJSON(user) {
Section {
AnyJSONView(value: user)
AnyJSONView(value: json)
}
}

if let user {
NavigationLink("Update profile") {
UpdateProfileView(user: user)
.navigationTitle("Update profile")
}
}

Expand Down
116 changes: 116 additions & 0 deletions Examples/Examples/Profile/UpdateProfileView.swift
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()
)
)
}
11 changes: 11 additions & 0 deletions Tests/IntegrationTests/AuthClientIntegrationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,17 @@ final class AuthClientIntegrationTests: XCTestCase {
}
}

func testSignInAnonymousAndLinkUserWithEmail() async throws {
try await XCTAssertAuthChangeEvents([.initialSession, .signedIn, .userUpdated]) {
try await authClient.signInAnonymously()

let email = mockEmail()
let user = try await authClient.update(user: UserAttributes(email: email))

XCTAssertEqual(user.newEmail, email)
}
}

func testDeleteAccountAndSignOut() async throws {
let response = try await signUpIfNeededOrSignIn(email: mockEmail(), password: mockPassword())

Expand Down

0 comments on commit 6f984ac

Please sign in to comment.