From faa233551de9114c012a7595b4ce0f4924454922 Mon Sep 17 00:00:00 2001 From: John Coffey Date: Mon, 16 Nov 2026 08:33:59 -0700 Subject: [PATCH] Keep the session when only the access token has expired The route guard counted an admin as signed in only while the hour-long access token was valid, so any fresh load after that went to the password page even though the refresh token behind it was still good. On a phone, where Android reloads an installed app when it comes back, that meant signing in again nearly every time. The guard now also accepts a stored refresh token. The first request renews the access token, and a refused renewal ends the session as before. Fixes #214 --- src/components/layout/ProtectedRoute.tsx | 9 ++++++++- src/stores/authStore.test.ts | 19 +++++++++++++++++++ src/stores/authStore.ts | 10 ++++++++++ 3 files changed, 37 insertions(+), 1 deletion(-) diff --git a/src/components/layout/ProtectedRoute.tsx b/src/components/layout/ProtectedRoute.tsx index a6431d5..aeef871 100644 --- a/src/components/layout/ProtectedRoute.tsx +++ b/src/components/layout/ProtectedRoute.tsx @@ -13,9 +13,16 @@ import { useAuthStore } from '@/stores/authStore'; * Shows its children to a signed-in admin; anyone else goes to the sign-in * page, which brings them back here afterwards. A development build with * VITE_ACCESS_TOKEN set counts as signed in. + * + * An access token lasts an hour, but the refresh token behind it lasts days, + * so an expired access token alone doesn't send anyone to sign in: the first + * request renews it, and a refused renewal ends the session there (#214). + * That's what reopening the console after a while looks like, and on a + * phone, where an installed app is reloaded whenever it comes back, it's + * every time. */ export function ProtectedRoute({ children }: { children: ReactNode }) { - const signedIn = useAuthStore((s) => s.isAuthenticated()) || !!import.meta.env.VITE_ACCESS_TOKEN; + const signedIn = useAuthStore((s) => s.hasSession()) || !!import.meta.env.VITE_ACCESS_TOKEN; const { pathname, search } = useLocation(); if (signedIn) return children; return ; diff --git a/src/stores/authStore.test.ts b/src/stores/authStore.test.ts index 345e945..ee63699 100644 --- a/src/stores/authStore.test.ts +++ b/src/stores/authStore.test.ts @@ -65,6 +65,25 @@ describe('tokens', () => { expect(auth().isAuthenticated()).toBe(false); }); + it('keeps the session past the access token while it can be renewed (#214)', () => { + vi.useFakeTimers({ now: 0 }); + expect(auth().hasSession()).toBe(false); + auth().setTokens('acc', 'ref', 10, '/token'); + vi.setSystemTime(3_600_000); + expect(auth().isAuthenticated()).toBe(false); + expect(auth().hasSession()).toBe(true); + auth().logout(); + expect(auth().hasSession()).toBe(false); + }); + + it('has no session from an expired access token without a refresh token', () => { + vi.useFakeTimers({ now: 0 }); + useAuthStore.setState({ accessToken: 'acc', refreshToken: null, tokenExpiresAt: 10_000, tokenEndpoint: '/token' }); + expect(auth().hasSession()).toBe(true); + vi.setSystemTime(10_000); + expect(auth().hasSession()).toBe(false); + }); + it('counts a token as expiring inside its last minute', () => { vi.useFakeTimers({ now: 0 }); expect(auth().isTokenExpiringSoon()).toBe(false); diff --git a/src/stores/authStore.ts b/src/stores/authStore.ts index 5eb24af..6042ee0 100644 --- a/src/stores/authStore.ts +++ b/src/stores/authStore.ts @@ -56,6 +56,11 @@ interface AuthState extends Signin { setUsername: (username: string | null) => void; logout: () => void; isAuthenticated: () => boolean; + /** + * Signed in, or able to renew the sign-in without asking: an access token + * that has run out still comes with a refresh token that lasts far longer. + */ + hasSession: () => boolean; /** Under a minute left, so the next request refreshes first. */ isTokenExpiringSoon: () => boolean; } @@ -159,6 +164,11 @@ export const useAuthStore = create()( return accessToken !== null && tokenExpiresAt !== null && Date.now() < tokenExpiresAt; }, + hasSession: () => { + const { refreshToken, tokenEndpoint } = get(); + return get().isAuthenticated() || (refreshToken !== null && tokenEndpoint !== null); + }, + isTokenExpiringSoon: () => { const { tokenExpiresAt } = get(); return tokenExpiresAt !== null && tokenExpiresAt - Date.now() < REFRESH_AHEAD_MS; -- 2.54.0