fix: access token refresh on web app (#5681)

This commit is contained in:
Peter Etelej 2026-03-05 14:29:21 +03:00 committed by GitHub
parent 334dfef714
commit 3010f10eaf
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 11 additions and 6 deletions

View File

@ -1,7 +1,7 @@
import { timestampDate } from "@bufbuild/protobuf/wkt";
import { Code, ConnectError, createClient, type Interceptor } from "@connectrpc/connect";
import { createConnectTransport } from "@connectrpc/connect-web";
import { getAccessToken, isTokenExpired, REQUEST_TOKEN_EXPIRY_BUFFER_MS, setAccessToken } from "./auth-state";
import { getAccessToken, hasStoredToken, isTokenExpired, REQUEST_TOKEN_EXPIRY_BUFFER_MS, setAccessToken } from "./auth-state";
import { ActivityService } from "./types/proto/api/v1/activity_service_pb";
import { AttachmentService } from "./types/proto/api/v1/attachment_service_pb";
import { AuthService } from "./types/proto/api/v1/auth_service_pb";
@ -124,7 +124,13 @@ async function refreshAndGetAccessToken(): Promise<string> {
async function getRequestToken(): Promise<string | null> {
let token = getAccessToken();
if (!token) {
return null;
if (!hasStoredToken()) return null;
try {
token = await refreshAndGetAccessToken();
} catch {
return null;
}
return token;
}
// Preflight refresh: avoid sending requests with expired access tokens.

View File

@ -1,5 +1,5 @@
import { useEffect } from "react";
import { FOCUS_TOKEN_EXPIRY_BUFFER_MS, getAccessToken, isTokenExpired } from "@/auth-state";
import { FOCUS_TOKEN_EXPIRY_BUFFER_MS, hasStoredToken, isTokenExpired } from "@/auth-state";
/**
* Hook that proactively refreshes the access token when the tab becomes visible
@ -20,9 +20,8 @@ export function useTokenRefreshOnFocus(refreshFn: () => Promise<void>, enabled:
return;
}
// Only refresh if we have a token
const token = getAccessToken();
if (!token) {
// Only refresh if the user has logged in before (token in localStorage)
if (!hasStoredToken()) {
return;
}