Skip to content

Commit 9363ffb

Browse files
authored
refactor: Replace Recoil with Zustand for auth state management (#101)
1 parent 544963f commit 9363ffb

File tree

8 files changed

+19
-29
lines changed

8 files changed

+19
-29
lines changed

bun.lockb

-204 Bytes
Binary file not shown.

src/App.tsx

+1-14
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,9 @@
1-
import { isLoginState } from 'atom/recoilStore';
21
import { RouterProvider } from 'react-router';
3-
import type { MutableSnapshot } from 'recoil';
4-
import { RecoilRoot } from 'recoil';
5-
import { getAccessToken } from 'utils/tokenManager';
62

73
import { router } from './__generated__/routes.generated';
84

95
const App = () => {
10-
const initialState = ({ set }: MutableSnapshot) => {
11-
const isLogin = !!getAccessToken();
12-
set(isLoginState, isLogin || false);
13-
};
14-
15-
return (
16-
<RecoilRoot initializeState={initialState}>
17-
<RouterProvider router={router} />
18-
</RecoilRoot>
19-
);
6+
return <RouterProvider router={router} />;
207
};
218

229
export default App;

src/atom/recoilStore.ts

-6
This file was deleted.

src/hooks/useHttp.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@ import axios from 'axios';
22
import { TOKEN_KEY } from 'constants/auth';
33
import jwtDecode, { type JwtPayload } from 'jwt-decode';
44
import { useEffect } from 'react';
5+
import { useAuthStore } from 'stores/authStore';
56
import { getAccessToken, setToken } from 'utils/tokenManager';
67

78
import { logout, refresh } from '../api/etc';
8-
import useUserStore from './useUserStore';
99

1010
const PROXY_URL = window.location.hostname === 'localhost' ? '/api' : '/proxy';
1111

@@ -15,7 +15,7 @@ const http = axios.create({
1515
});
1616

1717
const useHttp = () => {
18-
const { isLogin } = useUserStore();
18+
const { isLogin } = useAuthStore();
1919

2020
//액세스토큰 유효성 검사
2121
const isAccessTokenValid = async () => {

src/hooks/useUserStore.ts

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
import { useMutation, useQueryClient } from '@tanstack/react-query';
22
import { login as loginApi, refresh as refreshApi } from 'api/Auth';
3-
import { isLoginState } from 'atom/recoilStore';
43
import type { AxiosError } from 'axios';
54
import { REFRESH_KEY, TOKEN_KEY } from 'constants/auth';
65
import { useLocation } from 'react-router';
7-
import { useRecoilState } from 'recoil';
6+
import { useAuthStore } from 'stores/authStore';
87
import type { APIErrorResponse } from 'types/common';
98
import type { UserLogin } from 'types/user';
109
import { removeTokenAll, setToken } from 'utils/tokenManager';
@@ -13,12 +12,10 @@ import useRouter from './useRouter';
1312

1413
const useUserStore = () => {
1514
const queryClient = useQueryClient();
16-
15+
const { isLogin, setIsLogin } = useAuthStore();
1716
const { push } = useRouter();
1817
const { state } = useLocation();
1918

20-
const [isLogin, setIsLogin] = useRecoilState(isLoginState);
21-
2219
const { mutateAsync: login } = useMutation({
2320
mutationFn: (formData: UserLogin) => loginApi(formData),
2421
onSuccess: (data) => {

src/stores/authStore.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { getAccessToken } from 'utils/tokenManager';
2+
import { create } from 'zustand';
3+
4+
interface AuthState {
5+
isLogin: boolean;
6+
setIsLogin: (value: boolean) => void;
7+
}
8+
9+
export const useAuthStore = create<AuthState>((set) => ({
10+
isLogin: !!getAccessToken(),
11+
setIsLogin: (isLogin: boolean) => set({ isLogin }),
12+
}));

tsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,11 @@
1919
"paths": {
2020
"*": ["*"],
2121
"api/*": ["api/*"],
22-
"atom/*": ["atom/*"],
2322
"constants/*": ["constants/*"],
2423
"components/*": ["components/*"],
2524
"hooks/*": ["hooks/*"],
2625
"pages/*": ["pages/*"],
26+
"stores/*": ["stores/*"],
2727
"styles/*": ["styles/*"],
2828
"types/*": ["types/*"],
2929
"utils/*": ["utils/*"]

vite.config.mts

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ export default defineConfig({
1717
resolve: {
1818
alias: {
1919
api: '/src/api',
20-
atom: '/src/atom',
2120
constants: '/src/constants',
2221
components: '/src/components',
2322
hooks: '/src/hooks',
2423
pages: '/src/pages',
24+
stores: '/src/stores',
2525
styles: '/src/styles',
2626
types: '/src/types',
2727
utils: '/src/utils',

0 commit comments

Comments
 (0)