Skip auth on public routes

This commit is contained in:
CREDO23 2026-02-06 14:49:40 +02:00
parent d97068882a
commit 7192bb1a85
2 changed files with 42 additions and 20 deletions

View file

@ -1,16 +1,14 @@
import { atomWithQuery } from "jotai-tanstack-query";
import { userApiService } from "@/lib/apis/user-api.service";
import { getBearerToken } from "@/lib/auth-utils";
import { getBearerToken, isPublicRoute } from "@/lib/auth-utils";
import { cacheKeys } from "@/lib/query-client/cache-keys";
export const currentUserAtom = atomWithQuery(() => {
const pathname = typeof window !== "undefined" ? window.location.pathname : null;
return {
queryKey: cacheKeys.user.current(),
staleTime: 5 * 60 * 1000, // 5 minutes
// Only fetch user data when a bearer token is present
enabled: !!getBearerToken(),
queryFn: async () => {
return userApiService.getMe();
},
enabled: !!getBearerToken() && pathname !== null && !isPublicRoute(pathname),
queryFn: async () => userApiService.getMe(),
};
});