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;