Refactor: OAuthCallback 부분 리팩토링
This commit is contained in:
parent
95b06deecc
commit
c547860f62
@ -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;
|
||||
}
|
||||
|
19
frontend/src/hooks/useOAuthCallbackHooks.ts
Normal file
19
frontend/src/hooks/useOAuthCallbackHooks.ts
Normal 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);
|
||||
}
|
@ -92,7 +92,11 @@ const router = createBrowserRouter([
|
||||
},
|
||||
{
|
||||
path: webPath.oauthCallback(),
|
||||
element: <OAuthCallback />,
|
||||
element: (
|
||||
<Suspense fallback={<div></div>}>
|
||||
<OAuthCallback />
|
||||
</Suspense>
|
||||
),
|
||||
},
|
||||
]);
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user