diff --git a/frontend/src/components/OAuthCallback/index.tsx b/frontend/src/components/OAuthCallback/index.tsx index e5cf338..bc97a45 100644 --- a/frontend/src/components/OAuthCallback/index.tsx +++ b/frontend/src/components/OAuthCallback/index.tsx @@ -1,26 +1,18 @@ -import { useEffect } from 'react'; +import { useHandleOAuthCallback } from '@/hooks/useOAuthCallbackHooks'; import { useNavigate } from 'react-router-dom'; +import { useEffect } from 'react'; import useAuthStore from '@/stores/useAuthStore'; -import useProfileQuery from '@/queries/useProfileQuery'; export default function OAuthCallback() { - const queryParams = new URLSearchParams(window.location.search); - const accessToken = queryParams.get('accessToken'); + useHandleOAuthCallback(); const navigate = useNavigate(); - const setLoggedIn = useAuthStore((state) => state.setLoggedIn); - const setProfile = useAuthStore((state) => state.setProfile); - const { data: profile } = useProfileQuery(); + const profile = useAuthStore((state) => state.profile); useEffect(() => { - if (!accessToken) { - navigate('/'); - return; + if (profile) { + navigate('/browse', { replace: true }); } + }, [profile, navigate]); - setLoggedIn(true, accessToken); - setProfile(profile); - navigate('/browse'); - }, [accessToken, navigate, profile, setLoggedIn, setProfile]); - - return
처리 중입니다...
; + return null; } diff --git a/frontend/src/hooks/useOAuthCallbackHooks.ts b/frontend/src/hooks/useOAuthCallbackHooks.ts new file mode 100644 index 0000000..29b1cb9 --- /dev/null +++ b/frontend/src/hooks/useOAuthCallbackHooks.ts @@ -0,0 +1,19 @@ +import useAuthStore from '@/stores/useAuthStore'; +import { useSuspenseQuery } from '@tanstack/react-query'; +import { getProfile } from '@/api/authApi'; + +export function useHandleOAuthCallback() { + const queryParams = new URLSearchParams(window.location.search); + const accessToken = queryParams.get('accessToken'); + const setLoggedIn = useAuthStore((state) => state.setLoggedIn); + const setProfile = useAuthStore((state) => state.setProfile); + + accessToken && setLoggedIn(true, accessToken); + + const { data: profile } = useSuspenseQuery({ + queryKey: ['profile'], + queryFn: getProfile, + }); + + setProfile(profile); +} diff --git a/frontend/src/router/index.tsx b/frontend/src/router/index.tsx index abdb1bd..007d1c4 100644 --- a/frontend/src/router/index.tsx +++ b/frontend/src/router/index.tsx @@ -92,7 +92,11 @@ const router = createBrowserRouter([ }, { path: webPath.oauthCallback(), - element: