Sign-in and sign-out replace the page instead of adding to the history #222
No files matched your search
+14
-1
@@ -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 <Navigate to={to} replace />;
|
||||
}
|
||||
return <StartSignIn />;
|
||||
}
|
||||
|
||||
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).
|
||||
|
||||
@@ -52,7 +52,7 @@ function serve(routes: Record<string, unknown>) {
|
||||
/** 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();
|
||||
|
||||
@@ -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. */
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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/);
|
||||
});
|
||||
});
|
||||
@@ -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 });
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user