From 447837f58f4688ea7283eade0cb936ca9f5226eb Mon Sep 17 00:00:00 2001 From: Ademola Fadumo <48495111+demolaf@users.noreply.github.com> Date: Thu, 5 Nov 2026 14:51:18 +0100 Subject: [PATCH 1/3] fix(auth): forget email-link reauthentication when it is cancelled or no user is signed in Cancelling or dismissing EmailLinkReauthView left the stored reauthentication state set, so later sign-in links were skipped by the email-link screen and routed to reauthentication. Clear that state when the view closes, cancel the coordinator when the sheet is swiped away, and drop leftover state when no user is signed in. --- .../Sources/Services/AuthService.swift | 28 +++++++++-- .../Sources/Views/EmailLinkReauthScreen.swift | 12 ++++- .../Views/ReauthenticationModifier.swift | 6 ++- .../EmailLinkDeviceTests.swift | 49 +++++++++++++++++++ 4 files changed, 87 insertions(+), 8 deletions(-) diff --git a/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift b/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift index 0f54108e84..0a6fd2a48c 100644 --- a/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift +++ b/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift @@ -712,7 +712,7 @@ public extension AuthService { } let task: Task - if isReauthenticating { + if hasPendingEmailLinkReauth() { task = Task { try await self.handleEmailLinkReauthentication(url: url) } } else { guard url != lastSignedInEmailLink else { return } @@ -797,7 +797,7 @@ public extension AuthService { /// ``AuthView/emailLink`` when that screen isn't already showing to handle it itself. An app /// that shows its own screen through `withEmailLinkSignIn(onTap:)` handles links there. internal func presentEmailLinkScreen(for url: URL) { - guard emailLinkSignInEnabled, emailLinkUsesLibraryScreen, !isReauthenticating, + guard emailLinkSignInEnabled, emailLinkUsesLibraryScreen, !hasPendingEmailLinkReauth(), authenticationState != .authenticated, !(isPresented && authView == .emailLink), isEmailSignInLink(url) else { return } @@ -811,12 +811,30 @@ public extension AuthService { /// ``EmailLinkReauthView`` isn't showing, for example after a cold launch. Does nothing for /// other links, or when that view is showing to handle the link itself. internal func finishEmailLinkReauthentication(with url: URL) async throws { - // `auth.currentUser`, not `currentUser`: after a cold launch the link can arrive before the - // auth listener has filled in `currentUser`. - guard isReauthenticating, !isShowingEmailLinkReauth, auth.currentUser != nil else { return } + guard !isShowingEmailLinkReauth, hasPendingEmailLinkReauth() else { return } try await handleSignInLink(url: url) } + /// Forgets a pending email-link reauthentication, so links opened later are handled as + /// sign-in links. Called when ``EmailLinkReauthView`` is cancelled or dismissed. + internal func cancelEmailLinkReauthentication() { + emailLinkReauth = nil + isReauthenticating = false + } + + /// Whether a link the app opens is for email-link reauthentication. Forgets reauthentication + /// left over from a user who is no longer signed in, so a sign-in link isn't taken for it. + /// Reads `auth.currentUser`, which ``currentUser`` may not have caught up with after a cold + /// launch. + private func hasPendingEmailLinkReauth() -> Bool { + guard isReauthenticating else { return false } + guard let user = auth.currentUser, !user.isAnonymous else { + cancelEmailLinkReauthentication() + return false + } + return true + } + private func signInWithEmailLink(email: String, link: String, anonymousUserID: String?) async throws { diff --git a/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/EmailLinkReauthScreen.swift b/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/EmailLinkReauthScreen.swift index f39e1ba808..9834885ba5 100644 --- a/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/EmailLinkReauthScreen.swift +++ b/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/EmailLinkReauthScreen.swift @@ -98,7 +98,10 @@ public struct EmailLinkReauthScreen: View { VStack(spacing: 0) { content(EmailLinkReauthContentState( resendLink: { Task { await sendLink() } }, - cancel: { coordinator.reauthCancelled() }, + cancel: { + authService.cancelEmailLinkReauthentication() + coordinator.reauthCancelled() + }, email: email, linkSent: linkSent, isLoading: isLoading, @@ -113,7 +116,12 @@ public struct EmailLinkReauthScreen: View { } // `AuthPickerContent` leaves reauthentication links to this screen while it shows. .onAppear { authService.isShowingEmailLinkReauth = true } - .onDisappear { authService.isShowingEmailLinkReauth = false } + // Closing the screen abandons reauthentication. After it finishes, there is nothing left to + // forget. + .onDisappear { + authService.isShowingEmailLinkReauth = false + authService.cancelEmailLinkReauthentication() + } .task { await sendLink() } diff --git a/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/ReauthenticationModifier.swift b/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/ReauthenticationModifier.swift index f26e699be1..a2d9e82142 100644 --- a/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/ReauthenticationModifier.swift +++ b/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/ReauthenticationModifier.swift @@ -91,7 +91,11 @@ struct ReauthenticationModifier: ViewModifier { } } // Sheet for email link reauthentication - .sheet(isPresented: $coordinator.showingEmailLinkReauth, onDismiss: cancelIfPending) { + // Swiping the sheet away cancels, so the caller waiting on reauthentication isn't left + // hanging. Does nothing once reauthentication has finished or been cancelled. + .sheet(isPresented: $coordinator.showingEmailLinkReauth, onDismiss: { + coordinator.reauthCancelled() + }) { if case let .emailLink(context) = coordinator.reauthContext { EmailLinkReauthScreen(email: context.email, coordinator: coordinator) { ReauthPromptContent(prompt: .emailLink($0)) diff --git a/e2eTest/FirebaseSwiftUIExample/FirebaseSwiftUIExampleTests/EmailLinkDeviceTests.swift b/e2eTest/FirebaseSwiftUIExample/FirebaseSwiftUIExampleTests/EmailLinkDeviceTests.swift index b90cd4bdaf..316dfb081f 100644 --- a/e2eTest/FirebaseSwiftUIExample/FirebaseSwiftUIExampleTests/EmailLinkDeviceTests.swift +++ b/e2eTest/FirebaseSwiftUIExample/FirebaseSwiftUIExampleTests/EmailLinkDeviceTests.swift @@ -261,6 +261,55 @@ struct EmailLinkDeviceTests { try await relaunched.signOut() } + @Test + func cancelledReauthLeavesLaterLinksToSignIn() async throws { + let service = try await makeService() + let email = createEmail() + try await service.sendEmailSignInLink(email: email) + try await service.handleSignInLink(url: fetchEmailSignInLinkFromEmulator(email: email)) + try await waitForStateChange { + service.currentUser?.email?.caseInsensitiveCompare(email) == .orderedSame + } + + try await service.sendEmailSignInLink(email: email, isReauth: true) + #expect(service.isReauthenticating) + // What cancelling or dismissing `EmailLinkReauthView` does. + service.cancelEmailLinkReauthentication() + #expect(!service.isReauthenticating) + + // A relaunch doesn't bring the cancelled reauthentication back. + let relaunched = try await makeService(clearingEmulator: false) + #expect(!relaunched.isReauthenticating) + try await relaunched.signOut() + } + + @Test + func signInLinkIsNotTakenForReauthWithoutSignedInUser() async throws { + let service = try await makeService() + try service.auth.signOut() + // Reauthentication left over in storage, for example from a session that has since ended + // without `signOut()`. + try await service.sendEmailSignInLink(email: createEmail(), isReauth: true) + #expect(service.isReauthenticating) + + let email = createEmail() + try await service.sendEmailSignInLink(email: email) + let link = try await fetchEmailSignInLinkFromEmulator(email: email) + + service.presentEmailLinkScreen(for: link) + #expect(service.incomingEmailLink == link) + #expect(!service.isReauthenticating) + // Nothing to finish, so the picker leaves the link to the email-link screen. + try await service.finishEmailLinkReauthentication(with: link) + #expect(service.currentUser == nil) + + try await service.handleSignInLink(url: link) + try await waitForStateChange { + service.currentUser?.email?.caseInsensitiveCompare(email) == .orderedSame + } + try await service.signOut() + } + @Test func openedLinkPresentsEmailLinkScreen() throws { configureFirebaseIfNeeded() From b36041af4e9496563394f18d8256a62953d4a533 Mon Sep 17 00:00:00 2001 From: Ademola Fadumo <48495111+demolaf@users.noreply.github.com> Date: Tue, 10 Nov 2026 15:38:55 +0100 Subject: [PATCH 2/3] fix(auth): keep email-link reauthentication cleanup from undoing itself Forget the reauthentication a send stores after the view has closed, only cancel the coordinator on swipe-dismiss when no new request has started, and ignore leftover reauthentication without a signed-in user instead of clearing it, so a user that can't be read yet doesn't lose a pending one. --- .../Sources/Services/AuthService.swift | 16 ++++++---------- .../Sources/Views/EmailLinkReauthScreen.swift | 6 ++++++ .../Sources/Views/ReauthenticationModifier.swift | 8 ++++++-- .../EmailLinkDeviceTests.swift | 1 - 4 files changed, 18 insertions(+), 13 deletions(-) diff --git a/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift b/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift index 0a6fd2a48c..0e0a6839f0 100644 --- a/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift +++ b/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Services/AuthService.swift @@ -822,17 +822,13 @@ public extension AuthService { isReauthenticating = false } - /// Whether a link the app opens is for email-link reauthentication. Forgets reauthentication - /// left over from a user who is no longer signed in, so a sign-in link isn't taken for it. - /// Reads `auth.currentUser`, which ``currentUser`` may not have caught up with after a cold - /// launch. + /// Whether a link the app opens is for email-link reauthentication. Reauthentication left over + /// from a user who is no longer signed in doesn't count, so a sign-in link isn't taken for it. + /// It is kept rather than cleared, in case the user can't be read yet. Reads + /// `auth.currentUser`, which ``currentUser`` may not have caught up with after a cold launch. private func hasPendingEmailLinkReauth() -> Bool { - guard isReauthenticating else { return false } - guard let user = auth.currentUser, !user.isAnonymous else { - cancelEmailLinkReauthentication() - return false - } - return true + guard isReauthenticating, let user = auth.currentUser else { return false } + return !user.isAnonymous } private func signInWithEmailLink(email: String, diff --git a/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/EmailLinkReauthScreen.swift b/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/EmailLinkReauthScreen.swift index 9834885ba5..ed5a4e3392 100644 --- a/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/EmailLinkReauthScreen.swift +++ b/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/EmailLinkReauthScreen.swift @@ -137,6 +137,12 @@ public struct EmailLinkReauthScreen: View { defer { isLoading = false } do { try await authService.sendEmailSignInLink(email: email, isReauth: true) + // Closed while the email was sending: the send can't be stopped, so forget the + // reauthentication it just stored. + guard authService.isShowingEmailLinkReauth else { + authService.cancelEmailLinkReauthentication() + return + } linkSent = true } catch { report(error) diff --git a/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/ReauthenticationModifier.swift b/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/ReauthenticationModifier.swift index a2d9e82142..9b0030a653 100644 --- a/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/ReauthenticationModifier.swift +++ b/FirebaseSwiftUI/FirebaseAuthSwiftUI/Sources/Views/ReauthenticationModifier.swift @@ -92,9 +92,13 @@ struct ReauthenticationModifier: ViewModifier { } // Sheet for email link reauthentication // Swiping the sheet away cancels, so the caller waiting on reauthentication isn't left - // hanging. Does nothing once reauthentication has finished or been cancelled. + // hanging. Leaves alone a finished or cancelled reauthentication, and a new request made + // while the sheet was closing. .sheet(isPresented: $coordinator.showingEmailLinkReauth, onDismiss: { - coordinator.reauthCancelled() + if case .emailLink = coordinator.reauthContext, + !coordinator.showingEmailLinkReauthAlert { + cancelIfPending() + } }) { if case let .emailLink(context) = coordinator.reauthContext { EmailLinkReauthScreen(email: context.email, coordinator: coordinator) { diff --git a/e2eTest/FirebaseSwiftUIExample/FirebaseSwiftUIExampleTests/EmailLinkDeviceTests.swift b/e2eTest/FirebaseSwiftUIExample/FirebaseSwiftUIExampleTests/EmailLinkDeviceTests.swift index 316dfb081f..59e649c413 100644 --- a/e2eTest/FirebaseSwiftUIExample/FirebaseSwiftUIExampleTests/EmailLinkDeviceTests.swift +++ b/e2eTest/FirebaseSwiftUIExample/FirebaseSwiftUIExampleTests/EmailLinkDeviceTests.swift @@ -298,7 +298,6 @@ struct EmailLinkDeviceTests { service.presentEmailLinkScreen(for: link) #expect(service.incomingEmailLink == link) - #expect(!service.isReauthenticating) // Nothing to finish, so the picker leaves the link to the email-link screen. try await service.finishEmailLinkReauthentication(with: link) #expect(service.currentUser == nil) From c5f57f6737da34565b482ee87562e7aa48f323a6 Mon Sep 17 00:00:00 2001 From: Ademola Fadumo <48495111+demolaf@users.noreply.github.com> Date: Tue, 17 Nov 2026 16:24:55 +0100 Subject: [PATCH 3/3] docs(auth): limit the email-link reauthentication README note to an app closed mid-reauth --- FirebaseSwiftUI/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/FirebaseSwiftUI/README.md b/FirebaseSwiftUI/README.md index 2da8e5306d..1ea58b670d 100644 --- a/FirebaseSwiftUI/README.md +++ b/FirebaseSwiftUI/README.md @@ -279,7 +279,7 @@ When a sensitive operation requires reauthentication, the default views automati - **Email/Password**: Present a sheet prompting the user to enter their password before continuing. -- **Email Link**: Show an alert asking to send a verification email, then present a sheet with instructions to check email. The user taps the link in their email to complete reauthentication. `AuthPickerView` also completes it when the link opens the app after the sheet closed, for example after a cold launch. The action that asked for reauthentication isn't resumed then, so the user retries it. +- **Email Link**: Show an alert asking to send a verification email, then present a sheet with instructions to check email. The user taps the link in their email to complete reauthentication. `AuthPickerView` also completes it when the app was closed while the sheet was showing and the link reopens it. The action that asked for reauthentication isn't resumed then, so the user retries it. - **Phone**: Show an alert explaining verification is needed, then present a sheet for SMS code verification.