Skip to content

Commit ab76008

Browse files
committed
Remove MFA bits from LoginView.swift
1 parent ee561f4 commit ab76008

File tree

2 files changed

+21
-48
lines changed

2 files changed

+21
-48
lines changed

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

Lines changed: 20 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,16 @@ import FirebaseAuth
1818

1919
struct LoginView: View {
2020
@Environment(\.dismiss) private var dismiss
21-
@State private var multiFactorResolver: MultiFactorResolver? = nil
22-
@State private var onetimePasscode = ""
23-
@State private var showingAlert = false
2421

2522
@State private var email: String = ""
2623
@State private var password: String = ""
2724

25+
private weak var delegate: (any LoginDelegate)?
26+
27+
init(delegate: (any LoginDelegate)? = nil) {
28+
self.delegate = delegate
29+
}
30+
2831
var body: some View {
2932
Group {
3033
VStack {
@@ -74,12 +77,6 @@ struct LoginView: View {
7477
Spacer()
7578
}
7679
.padding()
77-
.alert("Enter one time passcode.", isPresented: $showingAlert) {
78-
TextField("Verification Code", text: $onetimePasscode)
79-
.textInputAutocapitalization(.never)
80-
Button("Cancel", role: .cancel) {}
81-
Button("Submit", action: submitOnetimePasscode)
82-
}
8380
}
8481

8582
private func login() {
@@ -88,25 +85,19 @@ struct LoginView: View {
8885
_ = try await AppManager.shared
8986
.auth()
9087
.signIn(withEmail: email, password: password)
91-
// } catch let error as AuthErrorCode
92-
// where error.code == .secondFactorRequired {
93-
// // error as? AuthErrorCode == nil because AuthErrorUtils returns generic
94-
// /Errors
95-
// // https://firebase.google.com/docs/auth/ios/totp-mfa#sign_in_users_with_a_second_factor
96-
// // TODO(ncooke3): Fix?
88+
// TODO(ncooke3): Investigate possible improvements.
89+
// } catch let error as AuthErrorCode
90+
// where error.code == .secondFactorRequired {
91+
// // error as? AuthErrorCode == nil because AuthErrorUtils returns generic
92+
// /Errors
93+
// // https://firebase.google.com/docs/auth/ios/totp-mfa#sign_in_users_with_a_second_factor
9794
} catch let error as NSError
9895
where error.code == AuthErrorCode.secondFactorRequired.rawValue {
9996
let mfaKey = AuthErrorUserInfoMultiFactorResolverKey
100-
guard
101-
let resolver = error.userInfo[mfaKey] as? MultiFactorResolver,
102-
let multiFactorInfo = resolver.hints.first
103-
else { return }
104-
if multiFactorInfo.factorID == TOTPMultiFactorID {
105-
// Show the alert to enter the TOTP verification code.
106-
multiFactorResolver = resolver
107-
showingAlert = true
108-
} else {
109-
// TODO(ncooke3): Implement handling of other MFA provider (phone).
97+
guard let resolver = error.userInfo[mfaKey] as? MultiFactorResolver else { throw error }
98+
await MainActor.run {
99+
dismiss()
100+
delegate?.loginDidOccur(resolver: resolver)
110101
}
111102
} catch {
112103
print(error.localizedDescription)
@@ -122,34 +113,16 @@ struct LoginView: View {
122113
password: password
123114
)
124115
// Sign-in was successful.
125-
dismiss()
116+
await MainActor.run {
117+
dismiss()
118+
delegate?.loginDidOccur(resolver: nil)
119+
}
126120
} catch {
127121
// TODO(ncooke3): Implement error display.
128122
print(error.localizedDescription)
129123
}
130124
}
131125
}
132-
133-
private func submitOnetimePasscode() {
134-
Task {
135-
guard onetimePasscode.count > 0 else { return }
136-
let multiFactorInfo = multiFactorResolver!.hints[0]
137-
let assertion = TOTPMultiFactorGenerator.assertionForSignIn(
138-
withEnrollmentID: multiFactorInfo.uid,
139-
// TODO(ncooke3): Probably should avoid network request if empty passcode.
140-
oneTimePassword: self.onetimePasscode
141-
)
142-
do {
143-
_ = try await multiFactorResolver!.resolveSignIn(with: assertion)
144-
// MFA login was successful.
145-
dismiss()
146-
} catch {
147-
// Wrong or expired OTP. Re-prompt the user.
148-
// TODO(ncooke3): Show error to user.
149-
print(error)
150-
}
151-
}
152-
}
153126
}
154127

155128
private struct SymbolTextField: TextFieldStyle {

FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/AuthViewController.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ class AuthViewController: UIViewController, DataSourceProviderDelegate {
338338
}
339339

340340
private func performDemoEmailPasswordLoginFlow() {
341-
let loginView = LoginView()
341+
let loginView = LoginView(delegate: self)
342342
let hostingController = UIHostingController(rootView: loginView)
343343
navigationController?.pushViewController(hostingController, animated: true)
344344
}

0 commit comments

Comments
 (0)