Skip to content

Commit c71e9fe

Browse files
committed
Refactor out login button action handler
1 parent 49dfae1 commit c71e9fe

File tree

1 file changed

+35
-31
lines changed
  • FirebaseAuth/Tests/SampleSwift/AuthenticationExample/CustomViews

1 file changed

+35
-31
lines changed

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

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -59,37 +59,9 @@ struct LoginView: View {
5959
LoginViewButton(
6060
text: "Login",
6161
accentColor: .white,
62-
backgroundColor: .orange
63-
) {
64-
Task {
65-
do {
66-
_ = try await AppManager.shared
67-
.auth()
68-
.signIn(withEmail: email, password: password)
69-
// } catch let error as AuthErrorCode
70-
// where error.code == .secondFactorRequired {
71-
// // error as? AuthErrorCode == nil because AuthErrorUtils returns generic Errors
72-
// // https://firebase.google.com/docs/auth/ios/totp-mfa#sign_in_users_with_a_second_factor
73-
// // TODO(ncooke3): Fix?
74-
} catch let error as NSError
75-
where error.code == AuthErrorCode.secondFactorRequired.rawValue {
76-
let mfaKey = AuthErrorUserInfoMultiFactorResolverKey
77-
guard
78-
let resolver = error.userInfo[mfaKey] as? MultiFactorResolver,
79-
let multiFactorInfo = resolver.hints.first
80-
else { return }
81-
if multiFactorInfo.factorID == TOTPMultiFactorID {
82-
// Show the alert to enter the TOTP verification code.
83-
multiFactorResolver = resolver
84-
showingAlert = true
85-
} else {
86-
// TODO(ncooke3): Implement handling of other MFA provider (phone).
87-
}
88-
} catch {
89-
print(error.localizedDescription)
90-
}
91-
}
92-
}
62+
backgroundColor: .orange,
63+
action: login
64+
)
9365
.alert("Enter one time passcode.", isPresented: $showingAlert) {
9466
TextField("Verification Code", text: $onetimePasscode)
9567
.textInputAutocapitalization(.never)
@@ -142,6 +114,38 @@ struct LoginView: View {
142114
}
143115
.padding()
144116
}
117+
118+
private func login() {
119+
Task {
120+
do {
121+
_ = try await AppManager.shared
122+
.auth()
123+
.signIn(withEmail: email, password: password)
124+
// } catch let error as AuthErrorCode
125+
// where error.code == .secondFactorRequired {
126+
// // error as? AuthErrorCode == nil because AuthErrorUtils returns generic
127+
// /Errors
128+
// // https://firebase.google.com/docs/auth/ios/totp-mfa#sign_in_users_with_a_second_factor
129+
// // TODO(ncooke3): Fix?
130+
} catch let error as NSError
131+
where error.code == AuthErrorCode.secondFactorRequired.rawValue {
132+
let mfaKey = AuthErrorUserInfoMultiFactorResolverKey
133+
guard
134+
let resolver = error.userInfo[mfaKey] as? MultiFactorResolver,
135+
let multiFactorInfo = resolver.hints.first
136+
else { return }
137+
if multiFactorInfo.factorID == TOTPMultiFactorID {
138+
// Show the alert to enter the TOTP verification code.
139+
multiFactorResolver = resolver
140+
showingAlert = true
141+
} else {
142+
// TODO(ncooke3): Implement handling of other MFA provider (phone).
143+
}
144+
} catch {
145+
print(error.localizedDescription)
146+
}
147+
}
148+
}
145149
}
146150

147151
private struct SymbolTextField: TextFieldStyle {

0 commit comments

Comments
 (0)