From 3fa4bf512b5a9eb1fb4bab68f95aca218a24925f Mon Sep 17 00:00:00 2001 From: John Coffey Date: Mon, 16 Nov 2026 09:00:14 -0700 Subject: [PATCH] Sign-in and sign-out replace the page instead of adding to the history Each step of signing in or out pushed a history entry: the console's start of sign-in, the end of a session, and signing out. In the installed app, Back from the dashboard then went to the server's sign-in page (already used, so it failed) and to /login, which started another sign-in, so Back never left the app. Those steps now replace the current page. /login reached with a session in hand goes on into the console. With the server's sign-in page doing the same (inbuxa-server #403), the whole sign-in collapses into the dashboard's entry, and Back from there leaves the app. Fixes #215 --- src/pages/LoginPage.tsx | 15 ++++++++++- src/services/auth/oauth.test.ts | 20 +++++++++++++- src/services/auth/oauth.ts | 5 +++- src/services/auth/session.ts | 3 ++- src/services/auth/signOut.test.ts | 43 +++++++++++++++++++++++++++++++ src/services/auth/signOut.ts | 13 +++++++--- 6 files changed, 91 insertions(+), 8 deletions(-) create mode 100644 src/services/auth/signOut.test.ts diff --git a/src/pages/LoginPage.tsx b/src/pages/LoginPage.tsx index 5ebae9d..036ff2c 100644 --- a/src/pages/LoginPage.tsx +++ b/src/pages/LoginPage.tsx @@ -7,7 +7,7 @@ import { ArrowRight, Loader2 } from 'lucide-react'; import { useCallback, useEffect, useRef, useState } from 'react'; import { useTranslation } from 'react-i18next'; -import { useLocation } from 'react-router-dom'; +import { Navigate, useLocation } from 'react-router-dom'; import Logo from '@/components/common/Logo'; import { SourceLink } from '@/components/common/SourceLink'; @@ -16,6 +16,7 @@ import { Card, CardContent, CardHeader } from '@/components/ui/card'; import { useDocumentTitle } from '@/hooks/useDocumentTitle'; import { startAuthFlow } from '@/services/auth/oauth'; import { clearReturnPath, readReturnPath } from '@/services/auth/session'; +import { useAuthStore } from '@/stores/authStore'; /** * Goes straight on to the server's own sign-in page, which asks for the @@ -24,6 +25,18 @@ import { clearReturnPath, readReturnPath } from '@/services/auth/session'; * or when getting there failed, with a button to try again. */ export default function LoginPage() { + const { state } = useLocation(); + // Reached with a sign-in already in hand (Back, or a bookmark): go on into + // the console rather than start another (#215). + const [signedIn] = useState(() => useAuthStore.getState().hasSession()); + if (signedIn) { + const to = (state as { from?: string } | null)?.from ?? readReturnPath() ?? '/'; + return ; + } + return ; +} + +function StartSignIn() { const { t } = useTranslation(); const { state } = useLocation(); // The page to come back to: from the route guard, or kept when the server ended the session (endSession). diff --git a/src/services/auth/oauth.test.ts b/src/services/auth/oauth.test.ts index 2efd74e..945fb59 100644 --- a/src/services/auth/oauth.test.ts +++ b/src/services/auth/oauth.test.ts @@ -52,7 +52,7 @@ function serve(routes: Record) { /** Stands in for window.location, capturing where the browser is sent. */ function pretendLocation(pathname = '/Settings/x:Domain', search = '') { const original = window.location; - const sentTo = { href: '' }; + const sentTo = { href: '', replaced: false }; Object.defineProperty(window, 'location', { configurable: true, value: { @@ -65,6 +65,10 @@ function pretendLocation(pathname = '/Settings/x:Domain', search = '') { set href(v: string) { sentTo.href = v; }, + replace(v: string) { + sentTo.href = v; + sentTo.replaced = true; + }, }, }); return { sentTo, restore: () => Object.defineProperty(window, 'location', { configurable: true, value: original }) }; @@ -155,6 +159,20 @@ describe('startAuthFlow', () => { return new URL(loc.sentTo.href); } + it('replaces the console page in the history rather than adding to it (#215)', async () => { + serve({ + [`${SERVER}/.well-known/openid-configuration`]: config, + [`${SERVER}/api/discover/`]: config, + }); + const loc = pretendLocation(); + try { + await startAuthFlow(null); + } finally { + loc.restore(); + } + expect(loc.sentTo.replaced).toBe(true); + }); + it("sends the browser to the server's sign-in page with a PKCE challenge it can check", async () => { const to = await signIn(null); const stored = getStoredOAuthData(); diff --git a/src/services/auth/oauth.ts b/src/services/auth/oauth.ts index b94bbc8..c16bf84 100644 --- a/src/services/auth/oauth.ts +++ b/src/services/auth/oauth.ts @@ -199,7 +199,10 @@ export async function startAuthFlow(username: string | null, returnUrl?: string const scope = scopesFor(endpoints.scopes_supported); if (scope) query.set('scope', scope); - window.location.href = `${endpoints.authorization_endpoint}?${query}`; + // Replace, not push: the console page that started this has done its job, + // and Back from the console after signing in shouldn't land on it, or on a + // sign-in that has already been used (#215). + window.location.replace(`${endpoints.authorization_endpoint}?${query}`); } /** Trades the code from the callback for tokens. */ diff --git a/src/services/auth/session.ts b/src/services/auth/session.ts index f8f151c..3fef00a 100644 --- a/src/services/auth/session.ts +++ b/src/services/auth/session.ts @@ -25,8 +25,9 @@ function isAuthPath(path: string): boolean { /** Sign out because the server ended the session, coming back here after signing in again. */ export function endSession( + // Replace, not push: the page whose session ended isn't one to go Back to (#215). go: (url: string) => void = (url) => { - window.location.href = url; + window.location.replace(url); }, ): void { const basePath = getBasePath(); diff --git a/src/services/auth/signOut.test.ts b/src/services/auth/signOut.test.ts new file mode 100644 index 0000000..1ef74af --- /dev/null +++ b/src/services/auth/signOut.test.ts @@ -0,0 +1,43 @@ +/* + * SPDX-FileCopyrightText: 2026 Coffey Labs LLC + * + * SPDX-License-Identifier: AGPL-3.0-only + */ + +import { afterEach, describe, expect, it, vi } from 'vitest'; + +import { useAuthStore } from '@/stores/authStore'; + +import { signOut } from './signOut'; + +/* + * Signing out replaces the page it was done from in the history, so Back + * afterwards doesn't return to a console page that would only send the + * reader to sign in again (#215). + */ +describe('signOut', () => { + const original = window.location; + afterEach(() => { + Object.defineProperty(window, 'location', { configurable: true, value: original }); + useAuthStore.getState().logout(); + }); + + it('goes to the sign-in page in place of the current one', () => { + useAuthStore.getState().setTokens('acc', 'ref', 60, '/token'); + const navigate = vi.fn(); + signOut(navigate); + expect(navigate).toHaveBeenCalledWith('/login', { replace: true }); + expect(useAuthStore.getState().accessToken).toBeNull(); + }); + + it("replaces the page when it signs out at the provider's end-session endpoint", () => { + const replace = vi.fn(); + Object.defineProperty(window, 'location', { configurable: true, value: { ...original, replace } }); + useAuthStore.getState().setTokens('acc', 'ref', 60, '/token', 'https://id.example.com/logout'); + const navigate = vi.fn(); + signOut(navigate); + expect(navigate).not.toHaveBeenCalled(); + expect(replace).toHaveBeenCalledTimes(1); + expect(String(replace.mock.calls[0][0])).toMatch(/^https:\/\/id\.example\.com\/logout/); + }); +}); diff --git a/src/services/auth/signOut.ts b/src/services/auth/signOut.ts index 4ac7620..53c4c11 100644 --- a/src/services/auth/signOut.ts +++ b/src/services/auth/signOut.ts @@ -7,13 +7,18 @@ import { useAuthStore } from '@/stores/authStore'; import { buildEndSessionUrl, getPostLogoutRedirectUri } from './oauth'; -/** Sign out here, then at the identity provider when it has an end-session endpoint. Used by the account menu and the command palette. */ -export function signOut(navigate: (to: string) => void): void { +/** + * Sign out here, then at the identity provider when it has an end-session + * endpoint. Used by the account menu and the command palette. Both ways + * replace the current page in the history, so Back after signing out doesn't + * return to a console page that would only send the reader to sign in (#215). + */ +export function signOut(navigate: (to: string, options?: { replace?: boolean }) => void): void { const endSessionEndpoint = useAuthStore.getState().endSessionEndpoint; useAuthStore.getState().logout(); if (endSessionEndpoint) { - window.location.href = buildEndSessionUrl(endSessionEndpoint, getPostLogoutRedirectUri()); + window.location.replace(buildEndSessionUrl(endSessionEndpoint, getPostLogoutRedirectUri())); } else { - navigate('/login'); + navigate('/login', { replace: true }); } } -- 2.54.0