Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.")
Expand Down Expand Up @@ -113,6 +119,7 @@ extension MFALoginView {
// MFA login was successful.
await MainActor.run {
dismiss()
delegate?.loginDidOccur(resolver: nil)
}
} catch {
print(error)
Expand All @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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?)
}
Original file line number Diff line number Diff line change
Expand Up @@ -357,7 +357,10 @@
}

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)
}

Expand Down Expand Up @@ -1010,7 +1013,7 @@
alertController.addAction(submitAction)

// Present the alert controller
UIApplication.shared.windows.first?.rootViewController?.present(

Check warning on line 1016 in FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/AuthViewController.swift

View workflow job for this annotation

GitHub Actions / integration-tests (ObjCApiTests)

'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead

Check warning on line 1016 in FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/AuthViewController.swift

View workflow job for this annotation

GitHub Actions / integration-tests (SwiftApiTests)

'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead

Check warning on line 1016 in FirebaseAuth/Tests/SampleSwift/AuthenticationExample/ViewControllers/AuthViewController.swift

View workflow job for this annotation

GitHub Actions / integration-tests (AuthenticationExampleUITests)

'windows' was deprecated in iOS 15.0: Use UIWindowScene.windows on a relevant window scene instead
alertController,
animated: true,
completion: nil
Expand Down Expand Up @@ -1067,8 +1070,12 @@
// MARK: - LoginDelegate

extension AuthViewController: LoginDelegate {
public func loginDidOccur() {
transitionToUserViewController()
public func loginDidOccur(resolver: MultiFactorResolver?) {
if let resolver {
performMfaLoginFlow(resolver: resolver)
} else {
transitionToUserViewController()
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
})
}
}
Expand Down
Loading