Skip to content

Commit

Permalink
Feat: #80 zustand authStore에서 persist 미들웨어 제거
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoonyesol committed Aug 29, 2024
1 parent 64c8982 commit b73268f
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 24 deletions.
4 changes: 2 additions & 2 deletions src/mocks/services/authServiceHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const authServiceHandler = [
status: 200,
headers: {
Authorization: `Bearer ${accessToken}`,
'Set-Cookie': `refreshToken=${refreshToken}; Max-Age=${REFRESH_TOKEN_EXPIRATION / 1000}; HttpOnly; SameSite=Strict`,
'Set-Cookie': `refreshToken=${refreshToken}; Max-Age=${REFRESH_TOKEN_EXPIRATION / 1000}; HttpOnly; SameSite=Strict; Secure`,
},
});
}
Expand Down Expand Up @@ -67,7 +67,7 @@ const authServiceHandler = [
status: 200,
headers: {
Authorization: `Bearer ${newAccessToken}`,
'Set-Cookie': `refreshToken=${refreshToken}; Max-Age=${REFRESH_TOKEN_EXPIRATION / 1000}; HttpOnly; SameSite=Strict`,
'Set-Cookie': `refreshToken=${refreshToken}; Max-Age=${REFRESH_TOKEN_EXPIRATION / 1000}; HttpOnly; SameSite=Strict; Secure`,
},
});
}
Expand Down
4 changes: 3 additions & 1 deletion src/pages/user/SignInPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { useAuthStore } from '@/stores/useAuthStore';
import { login } from '@/services/authService';

export default function SignInPage() {
const { Login } = useAuthStore();
const { toastError } = useToast();
const navigate = useNavigate();
const {
Expand All @@ -29,14 +30,15 @@ export default function SignInPage() {
},
});

// TODO: react-query 코드 분리하기
const signIn = useMutation({
mutationFn: (data: UserSignInForm) => login(data),
onSuccess: (response) => {
const accessToken = response.headers.authorization;
if (!accessToken) return toastError('로그인에 실패했습니다.');

authAxios.defaults.headers.Authorization = accessToken;
useAuthStore.getState().Login(accessToken.replace('Bearer ', ''));
Login(accessToken.replace('Bearer ', ''));

navigate('/', { replace: true });
},
Expand Down
34 changes: 13 additions & 21 deletions src/stores/useAuthStore.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
/* eslint-disable import/prefer-default-export */
import { createStore } from 'zustand';
import { persist } from 'zustand/middleware';
import { create } from 'zustand';

type AuthStore = {
isAuthenticated: boolean;
Expand All @@ -11,22 +10,15 @@ type AuthStore = {
Logout: () => void;
};

export const useAuthStore = createStore(
persist<AuthStore>(
(set) => ({
isAuthenticated: false,
accessToken: null,
setAccessToken: (token: string) => set({ accessToken: token }),
// clearAccessToken: () => set({ accessToken: null }),
Login: (token: string) => {
set({ isAuthenticated: true, accessToken: token });
},
Logout: () => {
set({ isAuthenticated: false, accessToken: null });
},
}),
{
name: 'auth-storage',
},
),
);
export const useAuthStore = create<AuthStore>((set) => ({
isAuthenticated: false,
accessToken: null,
setAccessToken: (token: string) => set({ accessToken: token }),
// clearAccessToken: () => set({ accessToken: null }),
Login: (token: string) => {
set({ isAuthenticated: true, accessToken: token });
},
Logout: () => {
set({ isAuthenticated: false, accessToken: null });
},
}));

0 comments on commit b73268f

Please sign in to comment.