Skip to content

Commit

Permalink
Feat: #80 authStore AT 자동 만료 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
Yoonyesol committed Sep 4, 2024
1 parent c17e0c8 commit 23c58ba
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/mocks/services/authServiceHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ const authServiceHandler = [
return new HttpResponse(null, {
status: 200,
headers: {
Authorization: `Bearer ${accessToken}; Expires=${accessTokenExpiryDate}; Max-Age=${AUTH_SETTINGS.ACCESS_TOKEN_EXPIRATION / 1000}`,
Authorization: `Bearer ${accessToken}`,
'Set-Cookie': `refreshToken=${refreshToken}; HttpOnly; SameSite=Strict; Secure; Path=/; Expires=${refreshTokenExpiryDate}; Max-Age=${AUTH_SETTINGS.REFRESH_TOKEN_EXPIRATION / 1000}`,
},
});
Expand Down Expand Up @@ -79,7 +79,7 @@ const authServiceHandler = [
return new HttpResponse(null, {
status: 200,
headers: {
Authorization: `Bearer ${newAccessToken}; Expires=${accessTokenExpiryDate}; Max-Age=${AUTH_SETTINGS.ACCESS_TOKEN_EXPIRATION / 1000}`,
Authorization: `Bearer ${newAccessToken}`,
'Set-Cookie': `refreshToken=${refreshToken}; HttpOnly; SameSite=Strict; Secure; Path=/; Expires=${refreshTokenExpiryDate}; Max-Age=${AUTH_SETTINGS.REFRESH_TOKEN_EXPIRATION / 1000}`,
},
});
Expand Down
4 changes: 1 addition & 3 deletions src/pages/user/SignInPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import type { UserSignInForm } from '@/types/UserType';
import useToast from '@/hooks/useToast';
import { useAuthStore } from '@/stores/useAuthStore';
import { login } from '@/services/authService';
import { AUTH_SETTINGS } from '@/constants/settings';

export default function SignInPage() {
const { onLogin } = useAuthStore();
Expand All @@ -37,8 +36,7 @@ export default function SignInPage() {
if (!accessToken) return toastError('로그인에 실패했습니다.');

const token = accessToken.split(' ')[1];
const expiresAt = Date.now() + AUTH_SETTINGS.ACCESS_TOKEN_EXPIRATION;
onLogin(token, expiresAt);
onLogin(token);

navigate('/', { replace: true });
} catch (error) {
Expand Down
20 changes: 11 additions & 9 deletions src/stores/useAuthStore.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,30 @@
/* eslint-disable import/prefer-default-export */
import { create } from 'zustand';
import { AUTH_SETTINGS } from '@/constants/settings';

type AuthStore = {
isAuthenticated: boolean;
accessToken: string | null;
accessTokenExpiresAt: number | null;

setAccessToken: (token: string, expiresAt: number) => void;
onLogin: (token: string, expiresAt: number) => void;
setAccessToken: (token: string) => void;
onLogin: (token: string) => void;
onLogout: () => void;
};

export const useAuthStore = create<AuthStore>((set) => ({
isAuthenticated: false,
accessToken: null,
accessTokenExpiresAt: null,

setAccessToken: (token: string, expiresAt: number) => set({ accessToken: token, accessTokenExpiresAt: expiresAt }),
setAccessToken: (token: string) => set({ accessToken: token }),

onLogin: (token: string, expiresAt: number) => {
set({ isAuthenticated: true, accessToken: token, accessTokenExpiresAt: expiresAt });
onLogin: (token: string) => {
set({ isAuthenticated: true, accessToken: token });

setTimeout(() => {
set({ isAuthenticated: false, accessToken: null });
}, AUTH_SETTINGS.ACCESS_TOKEN_EXPIRATION);
},

onLogout: () => {
set({ isAuthenticated: false, accessToken: null, accessTokenExpiresAt: null });
set({ isAuthenticated: false, accessToken: null });
},
}));

0 comments on commit 23c58ba

Please sign in to comment.