Skip to content

Commit 116d23a

Browse files
committed
[skip ci] Refactor out login button action handler (3)
1 parent 48f361b commit 116d23a

File tree

1 file changed

+22
-20
lines changed
  • FirebaseAuth/Tests/SampleSwift/AuthenticationExample/CustomViews

1 file changed

+22
-20
lines changed

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

Lines changed: 22 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -66,26 +66,7 @@ struct LoginView: View {
6666
TextField("Verification Code", text: $onetimePasscode)
6767
.textInputAutocapitalization(.never)
6868
Button("Cancel", role: .cancel) {}
69-
Button("Submit") {
70-
Task {
71-
guard onetimePasscode.count > 0 else { return }
72-
let multiFactorInfo = multiFactorResolver!.hints[0]
73-
let assertion = TOTPMultiFactorGenerator.assertionForSignIn(
74-
withEnrollmentID: multiFactorInfo.uid,
75-
// TODO(ncooke3): Probably should avoid network request if empty passcode.
76-
oneTimePassword: self.onetimePasscode
77-
)
78-
do {
79-
_ = try await multiFactorResolver!.resolveSignIn(with: assertion)
80-
// MFA login was successful.
81-
dismiss()
82-
} catch {
83-
// Wrong or expired OTP. Re-prompt the user.
84-
// TODO(ncooke3): Show error to user.
85-
print(error)
86-
}
87-
}
88-
}
69+
Button("Submit", action: submitOnetimePasscode)
8970
}
9071

9172
LoginViewButton(
@@ -149,6 +130,27 @@ struct LoginView: View {
149130
}
150131
}
151132
}
133+
134+
private func submitOnetimePasscode() {
135+
Task {
136+
guard onetimePasscode.count > 0 else { return }
137+
let multiFactorInfo = multiFactorResolver!.hints[0]
138+
let assertion = TOTPMultiFactorGenerator.assertionForSignIn(
139+
withEnrollmentID: multiFactorInfo.uid,
140+
// TODO(ncooke3): Probably should avoid network request if empty passcode.
141+
oneTimePassword: self.onetimePasscode
142+
)
143+
do {
144+
_ = try await multiFactorResolver!.resolveSignIn(with: assertion)
145+
// MFA login was successful.
146+
dismiss()
147+
} catch {
148+
// Wrong or expired OTP. Re-prompt the user.
149+
// TODO(ncooke3): Show error to user.
150+
print(error)
151+
}
152+
}
153+
}
152154
}
153155

154156
private struct SymbolTextField: TextFieldStyle {

0 commit comments

Comments
 (0)