Keep the session when only the access token has expired #219

Merged
jcoffey-dev merged 1 commits from fix/214-refresh-token-guard into main 2026-11-16 15:56:00 +00:00
3 changed files with 37 additions and 1 deletions

No files matched your search

+8 -1
View File
@@ -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 <Navigate to="/login" replace state={{ from: pathname + search }} />;
+19
View File
@@ -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);
+10
View File
@@ -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<AuthState>()(
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;