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 { 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 <p> ...</p>;
return null;
}

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(),
element: <OAuthCallback />,
element: (
<Suspense fallback={<div></div>}>
<OAuthCallback />
</Suspense>
),
},
]);