From 1397873d81c0d47ae28defd05d373afe513d8b29 Mon Sep 17 00:00:00 2001 From: jhynsoo Date: Wed, 25 Sep 2024 16:58:14 +0900 Subject: [PATCH] =?UTF-8?q?Refactor:=20authstore=20=EB=A6=AC=ED=8C=A9?= =?UTF-8?q?=ED=86=A0=EB=A7=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- frontend/src/api/authApi.ts | 10 +++---- frontend/src/api/axiosConfig.ts | 3 +-- .../WorkspaceBrowseLayout/index.tsx | 6 ++--- frontend/src/hooks/useOAuthCallback.ts | 4 +-- frontend/src/pages/Home.tsx | 26 +++++-------------- .../src/queries/auth/useReissueTokenQuery.ts | 4 +-- frontend/src/router/index.tsx | 2 -- frontend/src/stores/useAuthStore.ts | 8 +++--- 8 files changed, 23 insertions(+), 40 deletions(-) diff --git a/frontend/src/api/authApi.ts b/frontend/src/api/authApi.ts index d13a41e..9a500ba 100644 --- a/frontend/src/api/authApi.ts +++ b/frontend/src/api/authApi.ts @@ -6,9 +6,9 @@ export async function reissueToken() { } export async function getProfile() { - return api - .get('/auth/profile', { - withCredentials: true, - }) - .then(({ data }) => data); + return api.get('/auth/profile').then(({ data }) => data); +} + +export async function logout() { + return api.post('/auth/logout').then(({ data }) => data); } diff --git a/frontend/src/api/axiosConfig.ts b/frontend/src/api/axiosConfig.ts index 600f8db..c6b8e14 100644 --- a/frontend/src/api/axiosConfig.ts +++ b/frontend/src/api/axiosConfig.ts @@ -27,9 +27,8 @@ api.interceptors.response.use( return api .post(REFRESH_URL) .then(({ data }) => { - console.log(data); const { accessToken } = data; - useAuthStore.getState().setLoggedIn(true, accessToken); + useAuthStore.getState().setToken(accessToken); if (error.config) { return api(error.config); } diff --git a/frontend/src/components/WorkspaceBrowseLayout/index.tsx b/frontend/src/components/WorkspaceBrowseLayout/index.tsx index d5def81..018bd50 100644 --- a/frontend/src/components/WorkspaceBrowseLayout/index.tsx +++ b/frontend/src/components/WorkspaceBrowseLayout/index.tsx @@ -8,15 +8,15 @@ import useWorkspaceListQuery from '@/queries/workspaces/useWorkspaceListQuery'; import useCreateWorkspaceQuery from '@/queries/workspaces/useCreateWorkspaceQuery'; export default function WorkspaceBrowseLayout() { - const { profile, isLoggedIn } = useAuthStore(); + const { profile } = useAuthStore(); const memberId = profile?.id ?? 0; const navigate = useNavigate(); useEffect(() => { - if (!isLoggedIn || memberId == 0) { + if (memberId == 0) { navigate('/'); } - }, [isLoggedIn, memberId, navigate]); + }, [memberId, navigate]); const { data: workspacesResponse } = useWorkspaceListQuery(memberId ?? 0); const createWorkspace = useCreateWorkspaceQuery(); diff --git a/frontend/src/hooks/useOAuthCallback.ts b/frontend/src/hooks/useOAuthCallback.ts index b25cca2..9281ea7 100644 --- a/frontend/src/hooks/useOAuthCallback.ts +++ b/frontend/src/hooks/useOAuthCallback.ts @@ -4,11 +4,11 @@ import useProfileQuery from '@/queries/auth/useProfileQuery'; export default function useHandleOAuthCallback() { const queryParams = new URLSearchParams(window.location.search); const accessToken = queryParams.get('accessToken'); - const setLoggedIn = useAuthStore((state) => state.setLoggedIn); + const setToken = useAuthStore((state) => state.setToken); const setProfile = useAuthStore((state) => state.setProfile); if (accessToken) { - setLoggedIn(true, accessToken); + setToken(accessToken); } const { data: profile } = useProfileQuery(); diff --git a/frontend/src/pages/Home.tsx b/frontend/src/pages/Home.tsx index 27f56fb..fc82aa1 100644 --- a/frontend/src/pages/Home.tsx +++ b/frontend/src/pages/Home.tsx @@ -1,25 +1,12 @@ -import { useRef } from 'react'; import { Link } from 'react-router-dom'; import GoogleLogo from '@/assets/icons/web_neutral_rd_ctn@1x.png'; import useAuthStore from '@/stores/useAuthStore'; import { Button } from '@/components/ui/button'; -import { getProfile } from '@/api/authApi'; + const BASE_URL = import.meta.env.VITE_API_URL; export default function Home() { - const { isLoggedIn, accessToken, setLoggedIn, profile, setProfile } = useAuthStore(); - const hasFetchedProfile = useRef(false); - - if (!isLoggedIn && !profile && !hasFetchedProfile.current && accessToken) { - setLoggedIn(true, accessToken); - getProfile().then((data) => { - setProfile(data); - hasFetchedProfile.current = true; - }); - } - const handleGoogleSignIn = () => { - window.location.href = `${BASE_URL}/login/oauth2/authorization/google`; - }; + const { accessToken } = useAuthStore(); return (
@@ -42,9 +29,10 @@ export default function Home() {

- {!isLoggedIn ? ( - // 404 에러 방지 + // 404 에러 방지 ) : ( <>