Repository navigation
feat(auth): add pickerContent and pickerDestination slots to AuthPickerView #1403
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
+361
−136
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
df93299
feat(auth): add pickerContent and pickerDestination slots to AuthPick…
demolaf 1f9a66e
fix(auth): keep signed-in and progress UI outside the pickerContent slot
demolaf a6caa62
docs(auth): add usage examples to AuthPickerView and authFont
demolaf f0fa077
fix(auth): let the authenticating overlay inherit the app font when n…
demolaf 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
156 changes: 156 additions & 0 deletions
156
FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/AuthPickerContent.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,156 @@ | ||
| // 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 FirebaseAuthUIComponents | ||
| import SwiftUI | ||
|
|
||
| /// An ``AuthPickerView`` with custom sheet content, returned by | ||
| /// ``AuthPickerView/pickerContent(_:)`` and ``AuthPickerView/pickerDestination(_:)``. | ||
| /// | ||
| /// It owns the authentication sheet, its `NavigationStack`, error alerts, and MFA and | ||
| /// account-conflict handling; only the first screen and the pushed screens come from the slots. | ||
| /// You don't create it directly. | ||
| @MainActor | ||
| public struct AuthPickerContent< | ||
| Content: View, | ||
| PickerContent: View, | ||
| DestinationContent: View | ||
| >: View { | ||
| init(content: @escaping () -> Content, | ||
| pickerContent: @escaping () -> PickerContent, | ||
| destination: @escaping (AuthView) -> DestinationContent) { | ||
| self.content = content | ||
| self.pickerContent = pickerContent | ||
| self.destination = destination | ||
| } | ||
|
|
||
| @Environment(AuthService.self) private var authService | ||
| private let content: () -> Content | ||
| private let pickerContent: () -> PickerContent | ||
| private let destination: (AuthView) -> DestinationContent | ||
|
|
||
| // View-layer error state | ||
| @State private var error: AlertError? | ||
|
|
||
| public var body: some View { | ||
| @Bindable var authService = authService | ||
| content() | ||
| .sheet(isPresented: $authService.isPresented) { | ||
| @Bindable var navigator = authService.navigator | ||
| NavigationStack(path: $navigator.routes) { | ||
| root | ||
| .navigationTitle(authService.authenticationState == .unauthenticated ? authService | ||
| .string.authPickerTitle : "") | ||
| .navigationBarTitleDisplayMode(.large) | ||
| .toolbar { | ||
| toolbar | ||
| } | ||
| .navigationDestination(for: AuthView.self) { view in | ||
| destination(view) | ||
| } | ||
| } | ||
| .environment(\.reportError, reportError) | ||
| .errorAlert( | ||
| error: $error, | ||
| okButtonLabel: authService.string.okButtonLabel | ||
| ) | ||
| .sheet(item: $authService.legacySignInRecovery) { _ in | ||
| LegacySignInRecoveryView() | ||
| .environment(authService) | ||
| } | ||
| .interactiveDismissDisabled(authService.configuration.interactiveDismissEnabled) | ||
| // Apply account conflict handling at NavigationStack level | ||
| .accountConflictHandler() | ||
| // Apply MFA handling at NavigationStack level | ||
| .mfaHandler() | ||
| .environment(authService) | ||
| } | ||
| } | ||
|
|
||
| /// The sheet's first screen. The slot only replaces the signed-out screen, so signed-in users | ||
| /// always reach ``SignedInView`` and the progress overlay always shows while signing in. | ||
| @ViewBuilder | ||
| private var root: some View { | ||
| VStack { | ||
| if authService.authenticationState == .authenticated { | ||
| SignedInView() | ||
| } else { | ||
| pickerContent() | ||
| } | ||
| } | ||
| .overlay { | ||
| if authService.authenticationState == .authenticating { | ||
| VStack(spacing: 24) { | ||
| ProgressView() | ||
| .scaleEffect(1.25) | ||
| .tint(.white) | ||
| Text("Authenticating...") | ||
| .authCustomFont(.body) | ||
| .foregroundStyle(.white) | ||
| } | ||
| .frame(maxWidth: .infinity, maxHeight: .infinity) | ||
| .background(.black.opacity(0.7)) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /// Replaces the signed-out first screen of the authentication sheet. See | ||
| /// ``AuthPickerView/pickerContent(_:)``. | ||
| public func pickerContent<NewPickerContent: View>( | ||
| @ViewBuilder _ pickerContent: @escaping () -> NewPickerContent | ||
| ) -> AuthPickerContent<Content, NewPickerContent, DestinationContent> { | ||
| AuthPickerContent<Content, NewPickerContent, DestinationContent>( | ||
| content: content, | ||
| pickerContent: pickerContent, | ||
| destination: destination | ||
| ) | ||
| } | ||
|
|
||
| /// Replaces the screens pushed inside the authentication sheet. See | ||
| /// ``AuthPickerView/pickerDestination(_:)``. | ||
| public func pickerDestination<NewDestinationContent: View>( | ||
| @ViewBuilder _ destination: @escaping (AuthView) -> NewDestinationContent | ||
| ) -> AuthPickerContent<Content, PickerContent, NewDestinationContent> { | ||
| AuthPickerContent<Content, PickerContent, NewDestinationContent>( | ||
| content: content, | ||
| pickerContent: pickerContent, | ||
| destination: destination | ||
| ) | ||
| } | ||
|
|
||
| /// Closure for reporting errors from child views | ||
| private func reportError(_ error: Error) { | ||
| Task { @MainActor in | ||
| self.error = AlertError( | ||
| message: authService.string.localizedErrorMessage(for: error), | ||
| underlyingError: error | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| @ToolbarContentBuilder | ||
| private var toolbar: some ToolbarContent { | ||
| ToolbarItem(placement: .topBarTrailing) { | ||
| if !authService.configuration.shouldHideCancelButton { | ||
| Button { | ||
| authService.isPresented = false | ||
| } label: { | ||
| Image(systemName: "xmark") | ||
| .foregroundStyle(Color(UIColor.label)) | ||
| } | ||
|
demolaf marked this conversation as resolved.
|
||
| } | ||
| } | ||
| } | ||
| } | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.