Skip to content

Commit cafb388

Browse files
committed
fix: prevent redirect to login on home page for unauthenticated users
1 parent 7023863 commit cafb388

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

packages/sdk/src/client.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ export const createApiClient = (config: ApiClientConfig): AxiosInstance => {
6161

6262
// If error is 401 and we haven't tried to refresh yet
6363
if (error.response?.status === 401 && !originalRequest._retry) {
64+
const refreshToken = getStoredRefreshToken();
65+
66+
// If no refresh token exists, user was never logged in - just reject
67+
if (!refreshToken) {
68+
return Promise.reject(error);
69+
}
70+
6471
if (isRefreshing) {
6572
// If already refreshing, queue this request
6673
return new Promise((resolve, reject) => {
@@ -77,8 +84,6 @@ export const createApiClient = (config: ApiClientConfig): AxiosInstance => {
7784
originalRequest._retry = true;
7885
isRefreshing = true;
7986

80-
const refreshToken = getStoredRefreshToken();
81-
8287
try {
8388
// Try to refresh the token
8489
const response = await axios.post(`${config.baseURL}/api/auth/refresh`, {
@@ -103,7 +108,7 @@ export const createApiClient = (config: ApiClientConfig): AxiosInstance => {
103108
// Retry the original request
104109
return client(originalRequest);
105110
} catch (refreshError) {
106-
// Refresh failed, clear tokens and redirect
111+
// Refresh failed, clear tokens and redirect to login
107112
processQueue(refreshError);
108113
isRefreshing = false;
109114
clearStoredToken();

packages/sdk/src/services/queries/auth/useMe/index.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,17 @@ import { useQuery } from '@tanstack/react-query';
22
import { useApiClient } from '@providers/ApiProvider';
33
import { useMeKey } from './key';
44
import { fetchMe } from './request';
5+
import { getStoredToken } from '../../../../client';
56

67
export function useMe() {
78
const { client } = useApiClient();
9+
const hasToken = !!getStoredToken();
810

911
return useQuery({
1012
queryKey: useMeKey(),
1113
queryFn: () => fetchMe(client),
1214
retry: false,
15+
enabled: hasToken, // Only fetch if user has a token
1316
});
1417
}
1518

0 commit comments

Comments
 (0)