Skip to content

Commit

Permalink
Remove MFA bits from LoginView.swift
Browse files Browse the repository at this point in the history
  • Loading branch information
ncooke3 committed Dec 11, 2024
1 parent ee561f4 commit ab76008
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ import FirebaseAuth

struct LoginView: View {
@Environment(\.dismiss) private var dismiss
@State private var multiFactorResolver: MultiFactorResolver? = nil
@State private var onetimePasscode = ""
@State private var showingAlert = false

@State private var email: String = ""
@State private var password: String = ""

private weak var delegate: (any LoginDelegate)?

init(delegate: (any LoginDelegate)? = nil) {
self.delegate = delegate
}

var body: some View {
Group {
VStack {
Expand Down Expand Up @@ -74,12 +77,6 @@ struct LoginView: View {
Spacer()
}
.padding()
.alert("Enter one time passcode.", isPresented: $showingAlert) {
TextField("Verification Code", text: $onetimePasscode)
.textInputAutocapitalization(.never)
Button("Cancel", role: .cancel) {}
Button("Submit", action: submitOnetimePasscode)
}
}

private func login() {
Expand All @@ -88,25 +85,19 @@ struct LoginView: View {
_ = try await AppManager.shared
.auth()
.signIn(withEmail: email, password: password)
// } catch let error as AuthErrorCode
// where error.code == .secondFactorRequired {
// // error as? AuthErrorCode == nil because AuthErrorUtils returns generic
// /Errors
// // https://firebase.google.com/docs/auth/ios/totp-mfa#sign_in_users_with_a_second_factor
// // TODO(ncooke3): Fix?
// TODO(ncooke3): Investigate possible improvements.
// } catch let error as AuthErrorCode
// where error.code == .secondFactorRequired {
// // error as? AuthErrorCode == nil because AuthErrorUtils returns generic
// /Errors
// // https://firebase.google.com/docs/auth/ios/totp-mfa#sign_in_users_with_a_second_factor
} catch let error as NSError
where error.code == AuthErrorCode.secondFactorRequired.rawValue {
let mfaKey = AuthErrorUserInfoMultiFactorResolverKey
guard
let resolver = error.userInfo[mfaKey] as? MultiFactorResolver,
let multiFactorInfo = resolver.hints.first
else { return }
if multiFactorInfo.factorID == TOTPMultiFactorID {
// Show the alert to enter the TOTP verification code.
multiFactorResolver = resolver
showingAlert = true
} else {
// TODO(ncooke3): Implement handling of other MFA provider (phone).
guard let resolver = error.userInfo[mfaKey] as? MultiFactorResolver else { throw error }
await MainActor.run {
dismiss()
delegate?.loginDidOccur(resolver: resolver)
}
} catch {
print(error.localizedDescription)
Expand All @@ -122,34 +113,16 @@ struct LoginView: View {
password: password
)
// Sign-in was successful.
dismiss()
await MainActor.run {
dismiss()
delegate?.loginDidOccur(resolver: nil)
}
} catch {
// TODO(ncooke3): Implement error display.
print(error.localizedDescription)
}
}
}

private func submitOnetimePasscode() {
Task {
guard onetimePasscode.count > 0 else { return }
let multiFactorInfo = multiFactorResolver!.hints[0]
let assertion = TOTPMultiFactorGenerator.assertionForSignIn(
withEnrollmentID: multiFactorInfo.uid,
// TODO(ncooke3): Probably should avoid network request if empty passcode.
oneTimePassword: self.onetimePasscode
)
do {
_ = try await multiFactorResolver!.resolveSignIn(with: assertion)
// MFA login was successful.
dismiss()
} catch {
// Wrong or expired OTP. Re-prompt the user.
// TODO(ncooke3): Show error to user.
print(error)
}
}
}
}

private struct SymbolTextField: TextFieldStyle {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ class AuthViewController: UIViewController, DataSourceProviderDelegate {
}

private func performDemoEmailPasswordLoginFlow() {
let loginView = LoginView()
let loginView = LoginView(delegate: self)
let hostingController = UIHostingController(rootView: loginView)
navigationController?.pushViewController(hostingController, animated: true)
}
Expand Down

0 comments on commit ab76008

Please sign in to comment.