From 57ef4109f7831bd4d88c4d2237c5fbadb3171b63 Mon Sep 17 00:00:00 2001 From: ValdiANS Date: Wed, 31 Dec 2025 10:34:07 +0700 Subject: [PATCH] feat: refresh user session --- src/components/helper/RequireAuth.tsx | 12 ++++++++++++ src/services/api/auth.ts | 15 +++++++++++++++ 2 files changed, 27 insertions(+) diff --git a/src/components/helper/RequireAuth.tsx b/src/components/helper/RequireAuth.tsx index 9dbd2557..aa7f81b2 100644 --- a/src/components/helper/RequireAuth.tsx +++ b/src/components/helper/RequireAuth.tsx @@ -5,6 +5,7 @@ import useSWR from 'swr'; import { useAuth } from '@/services/hooks/useAuth'; import { httpClientFetcher, SWRHttpKey } from '@/services/http/client'; +import { AuthApi } from '@/services/api/auth'; import { isResponseError, isResponseSuccess } from '@/lib/api-helper'; import { BaseApiResponse, GetMeResponse } from '@/types/api/api-general'; import { AxiosError } from 'axios'; @@ -55,6 +56,17 @@ const RequireAuth = ({ children }: RequireAuthProps) => { setIsLoadingUser(isLoadingUserResponse); }, [isLoadingUserResponse]); + useEffect(() => { + const interval = setInterval( + async () => { + await AuthApi.refresh(); + }, + 13 * 60 * 1000 + ); + + return () => clearInterval(interval); + }, []); + if ( (isLoadingUserResponse && !userResponse && !userErrorResponse) || (!userResponse && !userErrorResponse) diff --git a/src/services/api/auth.ts b/src/services/api/auth.ts index f5319435..ee27cf1d 100644 --- a/src/services/api/auth.ts +++ b/src/services/api/auth.ts @@ -18,6 +18,21 @@ export class AuthApiService { return undefined; } } + + async refresh() { + try { + const refreshRes = await httpClient(`/sso/refresh`, { + method: 'POST', + }); + + return refreshRes; + } catch (error) { + if (axios.isAxiosError(error)) { + return error.response?.data; + } + return undefined; + } + } } export const AuthApi = new AuthApiService();