From 1f04eeb04078621edc63a52a92b30d4f8dc0ef09 Mon Sep 17 00:00:00 2001 From: fallenbagel <98979876+fallenbagel@users.noreply.github.com> Date: Tue, 9 Dec 2025 20:17:17 +0800 Subject: [PATCH] fix: disable automatic auth revalidation on auth pages (#2213) * fix: disable automatic auth revalidation on auth pages Prevents unnecessary `/api/v1/auth/me` requests on login, setup, and password reset pages. fix #738 * fix: update regex to include resetpassword guid & add missing condition in refreshInterval --- src/hooks/useUser.ts | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/src/hooks/useUser.ts b/src/hooks/useUser.ts index 2a14ad1d5..d948f2de8 100644 --- a/src/hooks/useUser.ts +++ b/src/hooks/useUser.ts @@ -2,6 +2,7 @@ import { UserType } from '@server/constants/user'; import type { PermissionCheckOptions } from '@server/lib/permissions'; import { hasPermission, Permission } from '@server/lib/permissions'; import type { NotificationAgentKey } from '@server/lib/settings'; +import { useRouter } from 'next/router'; import type { MutatorCallback } from 'swr'; import useSWR from 'swr'; @@ -56,13 +57,21 @@ export const useUser = ({ id, initialData, }: { id?: number; initialData?: User } = {}): UserHookResponse => { + const router = useRouter(); + const isAuthPage = /^\/(login|setup|resetpassword(?:\/|$))/.test( + router.pathname + ); + const { data, error, mutate: revalidate, } = useSWR(id ? `/api/v1/user/${id}` : `/api/v1/auth/me`, { fallbackData: initialData, - refreshInterval: 30000, + refreshInterval: !isAuthPage ? 30000 : 0, + revalidateOnFocus: !isAuthPage, + revalidateOnMount: !isAuthPage, + revalidateOnReconnect: !isAuthPage, errorRetryInterval: 30000, shouldRetryOnError: false, });