diff --git a/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/CustomViews/MFALoginView.swift b/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/CustomViews/MFALoginView.swift index 7695d22af01..2f9d64f82c3 100644 --- a/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/CustomViews/MFALoginView.swift +++ b/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/CustomViews/MFALoginView.swift @@ -24,7 +24,13 @@ struct MFALoginView: View { // This is needed for both phone and TOTP MFA. @State private var verificationCode: String = "" - let resolver: MultiFactorResolver + private let resolver: MultiFactorResolver + private weak var delegate: (any LoginDelegate)? + + init(resolver: MultiFactorResolver, delegate: (any LoginDelegate)?) { + self.resolver = resolver + self.delegate = delegate + } var body: some View { Text("Choose a second factor to continue.") @@ -113,6 +119,7 @@ extension MFALoginView { // MFA login was successful. await MainActor.run { dismiss() + delegate?.loginDidOccur(resolver: nil) } } catch { print(error) @@ -131,6 +138,7 @@ extension MFALoginView { // MFA login was successful. await MainActor.run { dismiss() + delegate?.loginDidOccur(resolver: nil) } } catch { // Wrong or expired OTP. Re-prompt the user. diff --git a/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/Utility/LoginDelegate.swift b/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/Utility/LoginDelegate.swift index d1420d8923a..b76daa2a6a3 100644 --- a/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/Utility/LoginDelegate.swift +++ b/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/Utility/LoginDelegate.swift @@ -12,9 +12,10 @@ // See the License for the specific language governing permissions and // limitations under the License. +import FirebaseAuth import Foundation /// Delegate for signaling that a successful login with Firebase Auth has occurred protocol LoginDelegate: NSObject { - func loginDidOccur() + func loginDidOccur(resolver: MultiFactorResolver?) } diff --git a/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/AuthViewController.swift b/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/AuthViewController.swift index 936f4c76c25..7f8b77856f0 100644 --- a/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/AuthViewController.swift +++ b/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/AuthViewController.swift @@ -357,7 +357,10 @@ class AuthViewController: UIViewController, DataSourceProviderDelegate { } private func performMfaLoginFlow(resolver: MultiFactorResolver) { - let mfaLoginController = UIHostingController(rootView: MFALoginView(resolver: resolver)) + let mfaLoginController = UIHostingController(rootView: MFALoginView( + resolver: resolver, + delegate: self + )) present(mfaLoginController, animated: true) } @@ -1067,8 +1070,12 @@ class AuthViewController: UIViewController, DataSourceProviderDelegate { // MARK: - LoginDelegate extension AuthViewController: LoginDelegate { - public func loginDidOccur() { - transitionToUserViewController() + public func loginDidOccur(resolver: MultiFactorResolver?) { + if let resolver { + performMfaLoginFlow(resolver: resolver) + } else { + transitionToUserViewController() + } } } diff --git a/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/LoginController.swift b/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/LoginController.swift index 1ca3c9df197..36befea2bfd 100644 --- a/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/LoginController.swift +++ b/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/LoginController.swift @@ -64,15 +64,25 @@ class LoginController: UIViewController { private func login(with email: String, password: String) { AppManager.shared.auth().signIn(withEmail: email, password: password) { result, error in - guard error == nil else { return self.displayError(error) } - self.delegate?.loginDidOccur() + if let error { + let authError = error as NSError + if authError.code == AuthErrorCode.secondFactorRequired.rawValue { + let resolver = authError + .userInfo[AuthErrorUserInfoMultiFactorResolverKey] as! MultiFactorResolver + self.delegate?.loginDidOccur(resolver: resolver) + } else { + self.displayError(error) + } + } else { + self.delegate?.loginDidOccur(resolver: nil) + } } } private func createUser(email: String, password: String) { AppManager.shared.auth().createUser(withEmail: email, password: password) { authResult, error in guard error == nil else { return self.displayError(error) } - self.delegate?.loginDidOccur() + self.delegate?.loginDidOccur(resolver: nil) } } diff --git a/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/OtherAuthMethodControllers/CustomAuthViewController.swift b/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/OtherAuthMethodControllers/CustomAuthViewController.swift index da4248447de..0787a32447d 100644 --- a/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/OtherAuthMethodControllers/CustomAuthViewController.swift +++ b/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/OtherAuthMethodControllers/CustomAuthViewController.swift @@ -32,7 +32,7 @@ class CustomAuthViewController: OtherAuthViewController { AppManager.shared.auth().signIn(withCustomToken: token) { result, error in guard error == nil else { return self.displayError(error) } self.navigationController?.dismiss(animated: true, completion: { - self.delegate?.loginDidOccur() + self.delegate?.loginDidOccur(resolver: nil) }) } } diff --git a/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/OtherAuthMethodControllers/PasswordlessViewController.swift b/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/OtherAuthMethodControllers/PasswordlessViewController.swift index d710f323a6a..58609b9d925 100644 --- a/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/OtherAuthMethodControllers/PasswordlessViewController.swift +++ b/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/OtherAuthMethodControllers/PasswordlessViewController.swift @@ -67,7 +67,7 @@ class PasswordlessViewController: OtherAuthViewController { print("User verified with passwordless email.") self.navigationController?.dismiss(animated: true) { - self.delegate?.loginDidOccur() + self.delegate?.loginDidOccur(resolver: nil) } } else { print("User could not be verified by passwordless email") diff --git a/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/OtherAuthMethodControllers/PhoneAuthViewController.swift b/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/OtherAuthMethodControllers/PhoneAuthViewController.swift index a4e19077aad..f1a29ded31d 100644 --- a/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/OtherAuthMethodControllers/PhoneAuthViewController.swift +++ b/FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/OtherAuthMethodControllers/PhoneAuthViewController.swift @@ -47,7 +47,7 @@ class PhoneAuthViewController: OtherAuthViewController { AppManager.shared.auth().signIn(with: credential) { result, error in guard error == nil else { return self.displayError(error) } self.navigationController?.dismiss(animated: true, completion: { - self.delegate?.loginDidOccur() + self.delegate?.loginDidOccur(resolver: nil) }) } }