Skip to content
Open
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,12 @@ public enum AuthenticationFlow {

public enum AuthView: Hashable {
case passwordRecovery
/// A pushed email and password sign-in screen, ``SignInView`` by default. Reached with
/// ``AuthMethodPickerState/goToEmailSignIn``.
case signIn
/// A pushed email and password sign-up screen, ``SignUpView`` by default. Reached with
/// ``EmailAuthContentState/goToSignUp``.
case signUp
case emailLink
case updatePassword
case mfaEnrollment
Expand Down Expand Up @@ -159,6 +165,12 @@ public class Navigator {
public func clear() {
routes.removeAll()
}

/// Removes pushed ``AuthView/signIn`` and ``AuthView/signUp`` screens, which have nothing left
/// to do once the user is signed in.
func removeEmailRoutes() {
routes.removeAll { $0 == .signIn || $0 == .signUp }
}
}

@MainActor
Expand Down Expand Up @@ -292,6 +304,7 @@ public final class AuthService {
isReauthenticating = false
legacySignInRecovery = nil
suggestedEmailAddress = nil
navigator.clear()
updateAuthenticationState()
}

Expand Down Expand Up @@ -1243,7 +1256,13 @@ extension AuthService {

switch option.id {
case EmailAuthProviderID:
// An app whose first screen has no email form reached email sign-in through a pushed
// route, so return to that screen for the suggested address.
let usedEmailRoute = navigator.routes.contains { $0 == .signIn || $0 == .signUp }
navigator.clear()
if usedEmailRoute {
navigator.push(.signIn)
}
case emailLinkSignInMethod:
navigator.clear()
navigator.push(.emailLink)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,8 @@ public struct AuthPickerContent<
public var body: some View {
@Bindable var authService = authService
content()
.sheet(isPresented: $authService.isPresented) {
// Reopening the sheet starts from the first screen, not a half-finished pushed one.
.sheet(isPresented: $authService.isPresented, onDismiss: authService.navigator.clear) {
@Bindable var navigator = authService.navigator
NavigationStack(path: $navigator.routes) {
root
Expand Down Expand Up @@ -81,6 +82,13 @@ public struct AuthPickerContent<
.environment(authService)
}
.interactiveDismissDisabled(authService.configuration.interactiveDismissEnabled)
// Close pushed email screens however the user got signed in, including account
// conflicts the library resolves by signing in for them.
.onChange(of: authService.authenticationState) { _, state in
if state == .authenticated {
authService.navigator.removeEmailRoutes()
}
}
// Apply account conflict handling at NavigationStack level
.accountConflictHandler()
// Apply MFA handling at NavigationStack level
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,9 +71,9 @@ public struct AuthPickerView<Content: View>: View {
/// account conflicts and errors are handled the same as the default buttons.
///
/// Your screen replaces the logo, email sign-in, terms and the large "Sign in" title (the close
/// button stays), so show your own heading, email entry (for example one built around
/// ``EmailAuthView``) and any legal text you need. To keep the stock screen and only lay out
/// the provider buttons, use ``DefaultAuthPickerContent/init(authMethodPicker:)``.
/// button stays), so show your own heading, email entry (for example ``EmailAuthScreen``) and
/// any legal text you need. To keep the stock screen and only lay out the provider buttons, use
/// ``DefaultAuthPickerContent/init(authMethodPicker:)``.
///
/// Example:
/// ```swift
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ struct AuthMethodPickerStateReader<Content: View>: View {
providers: authService.registeredProviders,
select: selectProvider,
isEmailLinkEnabled: authService.emailLinkSignInEnabled,
selectEmailLink: authService.selectEmailLink
selectEmailLink: authService.selectEmailLink,
isEmailSignInEnabled: authService.emailPasswordSignInEnabled,
goToEmailSignIn: { authService.navigator.push(.signIn) }
))
}

Expand Down Expand Up @@ -137,15 +139,28 @@ public struct AuthMethodPickerState {
/// Starts email-link sign-in, the same as the stock email-link button.
public let selectEmailLink: @MainActor () -> Void

/// Whether email and password sign-in is enabled on the ``AuthService``.
public let isEmailSignInEnabled: Bool

/// Pushes ``AuthView/signIn``, an email and password sign-in screen of its own, for a first
/// screen that shows only provider buttons and a "Continue with email" button. Use it from
/// `AuthPickerView`'s whole-screen `pickerContent`; ``DefaultAuthPickerContent`` already shows
/// the email form.
public let goToEmailSignIn: @MainActor () -> Void

/// Creates a state, for example to preview a custom layout with stub actions.
public init(providers: [AuthProviderUI],
select: @escaping @MainActor (AuthProviderUI) -> Void,
isEmailLinkEnabled: Bool,
selectEmailLink: @escaping @MainActor () -> Void) {
selectEmailLink: @escaping @MainActor () -> Void,
isEmailSignInEnabled: Bool = false,
goToEmailSignIn: @escaping @MainActor () -> Void = {}) {
self.providers = providers
self.select = select
self.isEmailLinkEnabled = isEmailLinkEnabled
self.selectEmailLink = selectEmailLink
self.isEmailSignInEnabled = isEmailSignInEnabled
self.goToEmailSignIn = goToEmailSignIn
}

/// Hands a provider sign-in result to the same handlers the default provider buttons use:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ public struct DefaultAuthPickerDestination: View {
switch screen {
case AuthView.passwordRecovery:
PasswordRecoveryView()
case AuthView.signIn:
SignInView()
case AuthView.signUp:
SignUpView()
case AuthView.emailLink:
EmailLinkView()
case AuthView.updatePassword:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
// 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 FirebaseAuthUIComponents
import SwiftUI

private enum FocusableField: Hashable {
case email
case password
case confirmPassword
}

/// The stock content of ``EmailAuthScreen``.
@MainActor
public struct DefaultEmailAuthContent: View {
public init(state: EmailAuthContentState) {
self.state = state
}

@Environment(AuthService.self) private var authService
@FocusState private var focus: FocusableField?
private let state: EmailAuthContentState

public var body: some View {
VStack(spacing: 16) {
AuthTextField(
text: state.email,
label: authService.string.emailFieldLabel,
prompt: authService.string.emailInputLabel,
keyboardType: .emailAddress,
contentType: .emailAddress,
validations: [
FormValidators.email,
],
maintainsValidationMessage: state.flow == .signUp,
onSubmit: { _ in
self.focus = .password
},
leading: {
Image(systemName: "at")
}
)
.focused($focus, equals: .email)
.accessibilityIdentifier("email-field")
AuthTextField(
text: state.password,
label: authService.string.passwordFieldLabel,
prompt: authService.string.passwordInputLabel,
contentType: .password,
isSecureTextField: true,
validations: state.flow == .signUp ? [
FormValidators.atLeast6Characters
] : [],
maintainsValidationMessage: state.flow == .signUp,
onSubmit: { _ in
if state.flow == .signIn {
state.signIn()
} else {
focus = .confirmPassword
}
},
leading: {
Image(systemName: "lock")
}
)
.submitLabel(.go)
.focused($focus, equals: .password)
.accessibilityIdentifier("password-field")
if state.flow == .signIn {
Button(action: state.goToPasswordRecovery) {
Text(authService.string.passwordButtonLabel)
.authCustomFont(.body, weight: .medium)
.frame(maxWidth: .infinity, alignment: .trailing)
}
.accessibilityIdentifier("password-recovery-button")
}

if state.flow == .signUp {
AuthTextField(
text: state.confirmPassword,
label: authService.string.confirmPasswordFieldLabel,
prompt: authService.string.confirmPasswordInputLabel,
contentType: .password,
isSecureTextField: true,
validations: [
FormValidators.confirmPassword(password: state.password.wrappedValue),
],
maintainsValidationMessage: true,
onSubmit: { _ in
state.signUp()
},
leading: {
Image(systemName: "lock")
}
)
.submitLabel(.go)
.focused($focus, equals: .confirmPassword)
.accessibilityIdentifier("confirm-password-field")
}

Button {
if state.flow == .signIn {
state.signIn()
} else {
state.signUp()
}
} label: {
if !state.isLoading {
Text(
state.flow == .signIn
? authService.string.signInWithEmailButtonLabel
: authService.string.signUpWithEmailButtonLabel
)
.padding(.vertical, 8)
.frame(maxWidth: .infinity)
} else {
ProgressView()
.progressViewStyle(CircularProgressViewStyle(tint: .white))
.padding(.vertical, 8)
.frame(maxWidth: .infinity)
}
}
.disabled(!state.isValid)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The submit button is not disabled when state.isLoading is true. Although the screen's submit logic guards against multiple submissions, disabling the button during loading provides a better UX and prevents redundant tap interactions.

Suggested change
.disabled(!state.isValid)
.disabled(!state.isValid || state.isLoading)

.padding([.top, .bottom], 8)
.frame(maxWidth: .infinity)
.authCTAButtonStyle()
.accessibilityIdentifier("sign-in-button")
}
Button(action: state.switchFlow) {
HStack(spacing: 4) {
Text(
state.flow == .signIn
? authService.string.dontHaveAnAccountYetLabel
: authService.string.alreadyHaveAnAccountLabel
)
.authCustomFont(.body)
.foregroundStyle(Color(.label))
Text(
state.flow == .signUp
? authService.string.emailLoginFlowLabel
: authService.string.emailSignUpFlowLabel
)
.authCustomFont(.body, weight: .medium)
}
}
.accessibilityIdentifier("switch-auth-flow")
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
// 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 FirebaseAuthUIComponents
import SwiftUI

/// The stock content of ``EmailLinkScreen``.
@MainActor
public struct DefaultEmailLinkContent: View {
public init(state: EmailLinkContentState) {
self.state = state
}

@Environment(AuthService.self) private var authService
private let state: EmailLinkContentState

public var body: some View {
VStack(spacing: 24) {
AuthTextField(
text: state.email,
label: authService.string.signInLinkEmailFieldLabel,
prompt: authService.string.emailInputLabel,
keyboardType: .emailAddress,
contentType: .emailAddress,
validations: [
FormValidators.email,
],
leading: {
Image(systemName: "at")
}
)
.accessibilityIdentifier("email-link-email-field")
Button(action: state.sendSignInLink) {
Text(authService.string.sendEmailLinkButtonLabel)
.padding(.vertical, 8)
.frame(maxWidth: .infinity)
}
.authCTAButtonStyle()
.disabled(!state.isValid)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The submit button is not disabled when state.isLoading is true. Disabling the button during loading provides a better UX and prevents redundant tap interactions.

Suggested change
.disabled(!state.isValid)
.disabled(!state.isValid || state.isLoading)

.padding([.top, .bottom], 8)
.frame(maxWidth: .infinity)
}
.frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .top)
.navigationTitle(authService.string.signInWithEmailLinkViewTitle)
.safeAreaPadding()
.alert(
authService.string.signInWithEmailLinkViewTitle,
isPresented: Binding(
get: { state.didSend },
set: { isPresented in
if !isPresented {
state.dismissAlert()
}
}
)
) {
Button(authService.string.okButtonLabel) {}
} message: {
Text(authService.string.signInWithEmailLinkViewMessage)
}
}
}
Loading