Repository navigation
feat(auth): add content slots to MFA resolution and management screens #1416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
+825
−501
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
166 changes: 166 additions & 0 deletions
166
FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/DefaultMFAManagementContent.swift
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,166 @@ | ||
| // Copyright 2025 Google LLC | ||
| // | ||
| // Licensed under the Apache License, Version 2.0 (the "License"); | ||
| // you may not use this file except in compliance with the License. | ||
| // You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| import FirebaseAuth | ||
| import SwiftUI | ||
|
|
||
| /// The stock content of ``MFAManagementScreen``. | ||
| @MainActor | ||
| public struct DefaultMFAManagementContent: View { | ||
| public init(state: MFAManagementContentState) { | ||
| self.state = state | ||
| } | ||
|
|
||
| @Environment(AuthService.self) private var authService | ||
| private let state: MFAManagementContentState | ||
|
|
||
| public var body: some View { | ||
| VStack(spacing: 20) { | ||
| // Title section | ||
| VStack { | ||
| Text("Two-Factor Authentication") | ||
| .authFont(.largeTitle) | ||
| .fontWeight(.bold) | ||
| .multilineTextAlignment(.center) | ||
|
|
||
| Text("Manage your authentication methods") | ||
| .authFont(.subheadline) | ||
| .foregroundColor(.secondary) | ||
| .multilineTextAlignment(.center) | ||
| } | ||
| .padding(.horizontal) | ||
|
|
||
| if state.enrolledFactors.isEmpty { | ||
| // No factors enrolled | ||
| VStack(spacing: 16) { | ||
| Image(systemName: "shield.slash") | ||
| .font(.system(size: 48)) | ||
| .foregroundColor(.orange) | ||
|
|
||
| Text("No Authentication Methods") | ||
| .authFont(.title2) | ||
| .fontWeight(.semibold) | ||
|
|
||
| Text( | ||
| "Set up two-factor authentication to add an extra layer of security to your account." | ||
| ) | ||
| .authFont(.body) | ||
| .foregroundColor(.secondary) | ||
| .multilineTextAlignment(.center) | ||
| .padding(.horizontal) | ||
|
|
||
| Button { | ||
| state.goToEnrollment() | ||
| } label: { | ||
| Text("Set Up Two-Factor Authentication") | ||
| .padding(.vertical, 8) | ||
| .frame(maxWidth: .infinity) | ||
| } | ||
| .authCTAButtonStyle() | ||
| .padding([.top, .bottom], 8) | ||
| .frame(maxWidth: .infinity) | ||
| .accessibilityIdentifier("setup-mfa-button") | ||
| } | ||
| } else { | ||
| // Show enrolled factors | ||
| VStack(alignment: .leading, spacing: 16) { | ||
| Text("Enrolled Methods") | ||
| .authFont(.headline) | ||
| .padding(.horizontal) | ||
|
|
||
| ForEach(state.enrolledFactors) { factor in | ||
| factorRow(factor: factor) | ||
| } | ||
|
|
||
| Divider() | ||
| .padding(.horizontal) | ||
|
|
||
| Button("Add Another Method") { | ||
| state.goToEnrollment() | ||
| } | ||
| .padding([.top, .bottom], 8) | ||
| .frame(maxWidth: .infinity) | ||
| .authCTAButtonStyle() | ||
| .accessibilityIdentifier("add-mfa-method-button") | ||
| } | ||
| } | ||
|
|
||
| Spacer() | ||
| } | ||
| .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top) | ||
| .safeAreaPadding() | ||
| } | ||
|
|
||
| @ViewBuilder | ||
| private func factorRow(factor: MultiFactorInfo) -> some View { | ||
| HStack { | ||
| // Factor type icon | ||
| Group { | ||
| if factor.factorID == PhoneMultiFactorID { | ||
| Image(systemName: "message") | ||
| .foregroundColor(.blue) | ||
| } else { | ||
| Image(systemName: "qrcode") | ||
| .foregroundColor(.green) | ||
| } | ||
| } | ||
| .authFont(.title2) | ||
|
|
||
| VStack(alignment: .leading, spacing: 4) { | ||
| Text(factor.displayName ?? authService.string.unnamedMethodLabel) | ||
| .authFont(.headline) | ||
|
|
||
| if factor.factorID == PhoneMultiFactorID { | ||
| let phoneInfo = factor as! PhoneMultiFactorInfo | ||
| Text("SMS: \(phoneInfo.phoneNumber)") | ||
| .authFont(.caption) | ||
| .foregroundColor(.secondary) | ||
| } else { | ||
| Text("Authenticator App") | ||
| .authFont(.caption) | ||
| .foregroundColor(.secondary) | ||
| } | ||
|
|
||
| Text("Enrolled: \(DateFormatter.shortDate.string(from: factor.enrollmentDate))") | ||
| .authFont(.caption2) | ||
| .foregroundColor(.secondary) | ||
| } | ||
|
|
||
| Spacer() | ||
|
|
||
| Button("Remove") { | ||
| state.unenroll(factor.uid) | ||
| } | ||
| .buttonStyle(.bordered) | ||
| .foregroundColor(.red) | ||
| .disabled(state.isLoading) | ||
| .accessibilityIdentifier("remove-factor-\(factor.uid)") | ||
| } | ||
| .padding() | ||
| .background(Color(.systemGray6)) | ||
| .cornerRadius(8) | ||
| .padding(.horizontal) | ||
| } | ||
| } | ||
|
|
||
| // MARK: - Date Formatter Extension | ||
|
|
||
| private extension DateFormatter { | ||
| static let shortDate: DateFormatter = { | ||
| let formatter = DateFormatter() | ||
| formatter.dateStyle = .short | ||
| formatter.timeStyle = .none | ||
| return formatter | ||
| }() | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a force cast (
as!) can lead to runtime crashes if the underlying type is not what is expected (e.g., during testing or if SDK behavior changes). It is safer to use a conditional cast (as?) combined with the existingifcheck.