Skip to content

Commit c05a358

Browse files
committed
implement error handling
1 parent abe4d2f commit c05a358

File tree

1 file changed

+29
-10
lines changed
  • FirebaseAuth/Tests/SampleSwift/AuthenticationExample/CustomViews

1 file changed

+29
-10
lines changed

FirebaseAuth/Tests/SampleSwift/AuthenticationExample/CustomViews/LoginView.swift

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ struct LoginView: View {
2222
@State private var email: String = ""
2323
@State private var password: String = ""
2424

25+
// Properties for displaying error alerts.
26+
@State private var showingAlert: Bool = false
27+
@State private var error: Error?
28+
2529
private weak var delegate: (any LoginDelegate)?
2630

2731
init(delegate: (any LoginDelegate)? = nil) {
@@ -44,16 +48,22 @@ struct LoginView: View {
4448
// // error as? AuthErrorCode == nil because AuthErrorUtils returns generic
4549
// /Errors
4650
// // https://firebase.google.com/docs/auth/ios/totp-mfa#sign_in_users_with_a_second_factor
47-
} catch let error as NSError
48-
where error.code == AuthErrorCode.secondFactorRequired.rawValue {
49-
let mfaKey = AuthErrorUserInfoMultiFactorResolverKey
50-
guard let resolver = error.userInfo[mfaKey] as? MultiFactorResolver else { throw error }
51-
await MainActor.run {
52-
dismiss()
53-
delegate?.loginDidOccur(resolver: resolver)
54-
}
5551
} catch {
52+
let error = error as NSError
53+
if error.code == AuthErrorCode.secondFactorRequired.rawValue {
54+
let mfaKey = AuthErrorUserInfoMultiFactorResolverKey
55+
if let resolver = error.userInfo[mfaKey] as? MultiFactorResolver {
56+
// Multi-factor auth is required is to complete sign-in.
57+
await MainActor.run {
58+
dismiss()
59+
delegate?.loginDidOccur(resolver: resolver)
60+
}
61+
}
62+
}
63+
5664
print(error.localizedDescription)
65+
self.error = error
66+
self.showingAlert.toggle()
5767
}
5868
}
5969
}
@@ -71,8 +81,9 @@ struct LoginView: View {
7181
delegate?.loginDidOccur(resolver: nil)
7282
}
7383
} catch {
74-
// TODO(ncooke3): Implement error display.
7584
print(error.localizedDescription)
85+
self.error = error
86+
self.showingAlert.toggle()
7687
}
7788
}
7889
}
@@ -114,6 +125,14 @@ extension LoginView {
114125
Spacer()
115126
}
116127
.padding()
128+
.alert("Something went wrong.", isPresented: $showingAlert) {
129+
if let error {
130+
Text("Error: " + error.localizedDescription)
131+
}
132+
Button("OK", role: .cancel) {
133+
showingAlert.toggle()
134+
}
135+
}
117136
}
118137
}
119138

@@ -147,7 +166,7 @@ private struct CustomButtonStyle: ButtonStyle {
147166
.padding()
148167
.background(backgroundColor, in: RoundedRectangle(cornerRadius: 14))
149168
.foregroundStyle(foregroundColor)
150-
.opacity(configuration.isPressed ? 0.5 : 1) // is disabled?
169+
.opacity(configuration.isPressed ? 0.5 : 1)
151170
}
152171
}
153172

0 commit comments

Comments
 (0)