Skip to content

Commit

Permalink
[Auth] Extend MFA login flow to email/password page
Browse files Browse the repository at this point in the history
  • Loading branch information
ncooke3 committed Nov 15, 2024
1 parent 094e599 commit 54ac51a
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 11 deletions.
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 @@ 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)
}

Expand Down Expand Up @@ -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()
}
}
}

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

0 comments on commit 54ac51a

Please sign in to comment.