Refactor: OAuthCallback 부분 리팩토링

This commit is contained in:
정현조 2024-09-18 14:58:44 +09:00
parent 95b06deecc
commit c547860f62
3 changed files with 32 additions and 17 deletions

View File

@ -1,26 +1,18 @@
import { useEffect } from 'react'; import { useHandleOAuthCallback } from '@/hooks/useOAuthCallbackHooks';
import { useNavigate } from 'react-router-dom'; import { useNavigate } from 'react-router-dom';
import { useEffect } from 'react';
import useAuthStore from '@/stores/useAuthStore'; import useAuthStore from '@/stores/useAuthStore';
import useProfileQuery from '@/queries/useProfileQuery';
export default function OAuthCallback() { export default function OAuthCallback() {
const queryParams = new URLSearchParams(window.location.search); useHandleOAuthCallback();
const accessToken = queryParams.get('accessToken');
const navigate = useNavigate(); const navigate = useNavigate();
const setLoggedIn = useAuthStore((state) => state.setLoggedIn); const profile = useAuthStore((state) => state.profile);
const setProfile = useAuthStore((state) => state.setProfile);
const { data: profile } = useProfileQuery();
useEffect(() => { useEffect(() => {
if (!accessToken) { if (profile) {
navigate('/'); navigate('/browse', { replace: true });
return;
} }
}, [profile, navigate]);
setLoggedIn(true, accessToken); return null;
setProfile(profile);
navigate('/browse');
}, [accessToken, navigate, profile, setLoggedIn, setProfile]);
return <p> ...</p>;
} }

View File

@ -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);
}

View File

@ -92,7 +92,11 @@ const router = createBrowserRouter([
}, },
{ {
path: webPath.oauthCallback(), path: webPath.oauthCallback(),
element: <OAuthCallback />, element: (
<Suspense fallback={<div></div>}>
<OAuthCallback />
</Suspense>
),
}, },
]); ]);