Skip to content

Commit 9e23a3a

Browse files
refactor: move errorMessage to auth service
1 parent c9fd8fe commit 9e23a3a

7 files changed

Lines changed: 68 additions & 39 deletions

File tree

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift

Lines changed: 56 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ public final class AuthService {
8282
public var currentUser: User?
8383
public var authenticationState: AuthenticationState = .unauthenticated
8484
public var authenticationFlow: AuthenticationFlow = .login
85+
public var errorMessage = ""
8586

8687
private var listenerManager: AuthListenerManager?
8788
private let googleProvider: GoogleProviderProtocol?
@@ -132,15 +133,27 @@ public final class AuthService {
132133
}
133134

134135
public func updateAuthenticationState() {
136+
reset()
135137
authenticationState =
136138
(currentUser == nil || currentUser?.isAnonymous == true)
137139
? .unauthenticated
138140
: .authenticated
139141
}
140142

143+
func reset() {
144+
errorMessage = ""
145+
}
146+
141147
public func signOut() async throws {
142-
try await auth.signOut()
143-
updateAuthenticationState()
148+
do {
149+
try await auth.signOut()
150+
updateAuthenticationState()
151+
} catch {
152+
errorMessage = string.localizedErrorMessage(
153+
for: error
154+
)
155+
throw error
156+
}
144157
}
145158

146159
public func linkAccounts(credentials credentials: AuthCredential) async throws {
@@ -150,6 +163,9 @@ public final class AuthService {
150163
updateAuthenticationState()
151164
} catch {
152165
authenticationState = .unauthenticated
166+
errorMessage = string.localizedErrorMessage(
167+
for: error
168+
)
153169
throw error
154170
}
155171
}
@@ -165,6 +181,9 @@ public final class AuthService {
165181
updateAuthenticationState()
166182
} catch {
167183
authenticationState = .unauthenticated
184+
errorMessage = string.localizedErrorMessage(
185+
for: error
186+
)
168187
throw error
169188
}
170189
}
@@ -176,6 +195,9 @@ public final class AuthService {
176195
// TODO: - can use set user action code settings?
177196
try await currentUser!.sendEmailVerification()
178197
} catch {
198+
errorMessage = string.localizedErrorMessage(
199+
for: error
200+
)
179201
throw error
180202
}
181203
}
@@ -211,6 +233,9 @@ public extension AuthService {
211233
try await user.delete()
212234
}
213235
} catch {
236+
errorMessage = string.localizedErrorMessage(
237+
for: error
238+
)
214239
throw error
215240
}
216241
}
@@ -234,6 +259,9 @@ public extension AuthService {
234259
updateAuthenticationState()
235260
} catch {
236261
authenticationState = .unauthenticated
262+
errorMessage = string.localizedErrorMessage(
263+
for: error
264+
)
237265
throw error
238266
}
239267
}
@@ -242,6 +270,9 @@ public extension AuthService {
242270
do {
243271
try await auth.sendPasswordReset(withEmail: email)
244272
} catch {
273+
errorMessage = string.localizedErrorMessage(
274+
for: error
275+
)
245276
throw error
246277
}
247278
}
@@ -258,6 +289,9 @@ public extension AuthService {
258289
actionCodeSettings: actionCodeSettings
259290
)
260291
} catch {
292+
errorMessage = string.localizedErrorMessage(
293+
for: error
294+
)
261295
throw error
262296
}
263297
}
@@ -276,6 +310,9 @@ public extension AuthService {
276310
emailLink = nil
277311
}
278312
} catch {
313+
errorMessage = string.localizedErrorMessage(
314+
for: error
315+
)
279316
throw error
280317
}
281318
}
@@ -299,6 +336,9 @@ public extension AuthService {
299336
updateAuthenticationState()
300337
} catch {
301338
authenticationState = .unauthenticated
339+
errorMessage = string.localizedErrorMessage(
340+
for: error
341+
)
302342
throw error
303343
}
304344
}
@@ -316,6 +356,9 @@ public extension AuthService {
316356
updateAuthenticationState()
317357
} catch {
318358
authenticationState = .unauthenticated
359+
errorMessage = string.localizedErrorMessage(
360+
for: error
361+
)
319362
throw error
320363
}
321364
}
@@ -325,7 +368,14 @@ public extension AuthService {
325368

326369
public extension AuthService {
327370
func verifyPhoneNumber(phoneNumber: String) async throws -> String {
328-
return try await safePhoneAuthProvider.verifyPhoneNumber(phoneNumber: phoneNumber)
371+
do {
372+
return try await safePhoneAuthProvider.verifyPhoneNumber(phoneNumber: phoneNumber)
373+
} catch {
374+
errorMessage = string.localizedErrorMessage(
375+
for: error
376+
)
377+
throw error
378+
}
329379
}
330380

331381
func signInWithPhoneNumber(verificationID: String, verificationCode: String) async throws {
@@ -337,6 +387,9 @@ public extension AuthService {
337387
updateAuthenticationState()
338388
} catch {
339389
authenticationState = .unauthenticated
390+
errorMessage = string.localizedErrorMessage(
391+
for: error
392+
)
340393
throw error
341394
}
342395
}

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/AuthPickerView.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ extension AuthPickerView: View {
4040
.foregroundColor(.blue)
4141
}
4242
}
43+
Text(authService.errorMessage).foregroundColor(.red)
4344
}
4445
}
4546
}

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/EmailAuthView.swift

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public struct EmailAuthView {
2020
@State private var email = ""
2121
@State private var password = ""
2222
@State private var confirmPassword = ""
23-
@State private var errorMessage = ""
2423

2524
@FocusState private var focus: FocusableField?
2625

@@ -39,28 +38,21 @@ public struct EmailAuthView {
3938
try await authService.signIn(withEmail: email, password: password)
4039
} catch let error as NSError {
4140
switch AuthErrorCode(rawValue: error.code) {
42-
case .credentialAlreadyInUse:
41+
// case .credentialAlreadyInUse:
42+
default:
4343
// TODO: - how are we handling this?
4444
if let updatedCredential = error
4545
.userInfo[AuthErrorUserInfoUpdatedCredentialKey] as? AuthCredential {
4646
// user ought to merge accounts on their backend here
4747
}
48-
default:
49-
errorMessage = authService.string.localizedErrorMessage(
50-
for: error
51-
)
5248
}
5349
}
5450
}
5551

5652
private func createUserWithEmailPassword() async {
5753
do {
5854
try await authService.createUser(withEmail: email, password: password)
59-
} catch {
60-
errorMessage = authService.string.localizedErrorMessage(
61-
for: error
62-
)
63-
}
55+
} catch {}
6456
}
6557
}
6658

@@ -145,7 +137,6 @@ extension EmailAuthView: View {
145137
.environment(authService)) {
146138
Text("Prefer Email link sign-in?")
147139
}
148-
Text(errorMessage).foregroundColor(.red)
149140
}
150141
}
151142
}

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/EmailLinkView.swift

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import SwiftUI
44
public struct EmailLinkView {
55
@Environment(AuthService.self) private var authService
66
@State private var email = ""
7-
@State private var errorMessage = ""
87
@State private var showModal = false
98

109
public init() {}
@@ -13,9 +12,7 @@ public struct EmailLinkView {
1312
do {
1413
try await authService.sendEmailSignInLink(to: email)
1514
showModal = true
16-
} catch {
17-
errorMessage = error.localizedDescription
18-
}
15+
} catch {}
1916
}
2017
}
2118

@@ -47,7 +44,7 @@ extension EmailLinkView: View {
4744
.padding([.top, .bottom], 8)
4845
.frame(maxWidth: .infinity)
4946
.buttonStyle(.borderedProminent)
50-
Text(errorMessage).foregroundColor(.red)
47+
Text(authService.errorMessage).foregroundColor(.red)
5148
}.sheet(isPresented: $showModal) {
5249
VStack {
5350
Text("Instructions")
@@ -64,9 +61,7 @@ extension EmailLinkView: View {
6461
Task {
6562
do {
6663
try await authService.handleSignInLink(url: url)
67-
} catch {
68-
errorMessage = authService.string.localizedErrorMessage(for: error)
69-
}
64+
} catch {}
7065
}
7166
}
7267
}

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/PasswordRecoveryView.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import SwiftUI
33
public struct PasswordRecoveryView {
44
@Environment(AuthService.self) private var authService
55
@State private var email = ""
6-
@State private var errorMessage = ""
76
@State private var showModal = false
87

98
public init() {}
@@ -12,9 +11,7 @@ public struct PasswordRecoveryView {
1211
do {
1312
try await authService.sendPasswordRecoveryEmail(to: email)
1413
showModal = true
15-
} catch {
16-
errorMessage = error.localizedDescription
17-
}
14+
} catch {}
1815
}
1916
}
2017

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/SignedInView.swift

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ import SwiftUI
22

33
public struct SignedInView {
44
@Environment(AuthService.self) private var authService
5-
@State private var errorMessage = ""
65
}
76

87
extension SignedInView: View {
@@ -19,21 +18,17 @@ extension SignedInView: View {
1918
Task {
2019
do {
2120
try await authService.signOut()
22-
} catch {
23-
errorMessage = error.localizedDescription
24-
}
21+
} catch {}
2522
}
2623
}
2724
Divider()
2825
Button("Delete account") {
2926
Task {
3027
do {
3128
try await authService.deleteUser()
32-
} catch {
33-
errorMessage = error.localizedDescription
34-
}
29+
} catch {}
3530
}
3631
}
37-
Text(errorMessage).foregroundColor(.red)
32+
Text(authService.errorMessage).foregroundColor(.red)
3833
}
3934
}

FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/VerifyEmailView.swift

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,13 @@ import SwiftUI
22

33
public struct VerifyEmailView {
44
@Environment(AuthService.self) private var authService
5-
@State private var errorMessage = ""
65
@State private var showModal = false
76

87
private func sendEmailVerification() async {
98
do {
109
try await authService.sendEmailVerification()
1110
showModal = true
12-
} catch {
13-
errorMessage = error.localizedDescription
14-
}
11+
} catch {}
1512
}
1613
}
1714

0 commit comments

Comments
 (0)