Skip to content

Commit 44fd614

Browse files
authored
fix(fe): refactor authentication checks to use accessToken (#32)
* refactor: remove isLogin method from auth store * fix: replace isLogin method with accessToken checks in auth store
1 parent be9f4bc commit 44fd614

File tree

6 files changed

+15
-17
lines changed

6 files changed

+15
-17
lines changed

apps/client/src/components/Header.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import { Button, SignInModal, SignUpModal } from '@/components';
1010
function Header() {
1111
const { isLogin, clearAuthInformation: clearAccessToken } = useAuthStore(
1212
useShallow((state) => ({
13-
isLogin: state.isLogin,
13+
isLogin: state.accessToken != null,
1414
clearAuthInformation: state.clearAuthInformation,
1515
})),
1616
);
@@ -47,12 +47,12 @@ function Header() {
4747
<div className='flex items-center justify-center gap-2.5'>
4848
<Button
4949
className='hover:bg-gray-200 hover:text-white hover:transition-all'
50-
onClick={isLogin() ? handleLogout : openSignInModal}
50+
onClick={isLogin ? handleLogout : openSignInModal}
5151
>
52-
<p className='text-base font-bold text-black'>{isLogin() ? '로그아웃' : '로그인'}</p>
52+
<p className='text-base font-bold text-black'>{isLogin ? '로그아웃' : '로그인'}</p>
5353
</Button>
54-
<Button className='bg-indigo-600' onClick={isLogin() ? () => navigate({ to: '/my' }) : openSignUpModal}>
55-
<p className='text-base font-bold text-white'>{isLogin() ? '세션 기록' : '회원가입'}</p>
54+
<Button className='bg-indigo-600' onClick={isLogin ? () => navigate({ to: '/my' }) : openSignUpModal}>
55+
<p className='text-base font-bold text-white'>{isLogin ? '세션 기록' : '회원가입'}</p>
5656
</Button>
5757
</div>
5858
</div>

apps/client/src/features/auth/auth.store.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ import { create } from 'zustand';
33
interface AuthStore {
44
userId?: number;
55
accessToken?: string;
6-
isLogin: () => boolean;
76
setAuthInformation: ({ userId, accessToken }: { userId?: number; accessToken?: string }) => void;
87
clearAuthInformation: () => void;
98
}
109

11-
export const useAuthStore = create<AuthStore>((set, get) => ({
12-
isLogin: () => get().accessToken != null,
10+
export const useAuthStore = create<AuthStore>((set) => ({
1311
setAuthInformation: ({ userId, accessToken }: { userId?: number; accessToken?: string }) =>
1412
set({ userId, accessToken }),
1513
clearAuthInformation: () => set({ userId: undefined, accessToken: undefined }),

apps/client/src/features/session/session.util.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ export async function loadSessionData(options: LoadSessionOptions) {
3131
} = sessionStore;
3232

3333
// 1) [옵션] 로그인/refresh
34-
if (!skipRefresh && !authStore.isLogin()) {
34+
if (!skipRefresh && !authStore.accessToken) {
3535
try {
3636
const res = await refresh();
3737
authStore.setAuthInformation(res);

apps/client/src/pages/HomePage.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { useModal } from '@/features/modal';
77
import { Button, CreateSessionModal, FeatureCard } from '@/components';
88

99
function HomePage() {
10-
const isLogin = useAuthStore((state) => state.isLogin);
10+
const isLogin = useAuthStore((state) => state.accessToken != null);
1111

1212
const { Modal: CreateSession, openModal: openCreateSessionModal } = useModal(<CreateSessionModal />);
1313

@@ -25,12 +25,12 @@ function HomePage() {
2525
</div>
2626
<div className='group relative flex'>
2727
<Button
28-
className={`${isLogin() ? 'bg-indigo-600' : 'cursor-not-allowed bg-indigo-300'}`}
29-
onClick={isLogin() ? openCreateSessionModal : undefined}
28+
className={`${isLogin ? 'bg-indigo-600' : 'cursor-not-allowed bg-indigo-300'}`}
29+
onClick={isLogin ? openCreateSessionModal : undefined}
3030
>
3131
<div className='text-base font-bold text-white'>새로운 세션 만들기</div>
3232
</Button>
33-
{isLogin() ? undefined : (
33+
{isLogin ? undefined : (
3434
<span className='absolute top-1/2 flex w-full translate-y-full items-center justify-center rounded-md bg-indigo-600 p-1 text-sm font-bold text-white opacity-0 transition-opacity group-hover:opacity-100'>
3535
로그인 후 이용 가능
3636
</span>

apps/client/src/routes/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@ const LazyHomePage = React.lazy(() => import('@/pages').then((module) => ({ defa
77

88
export const Route = createFileRoute('/')({
99
beforeLoad: () => {
10-
const { isLogin, setAuthInformation } = useAuthStore.getState();
10+
const { accessToken, setAuthInformation } = useAuthStore.getState();
1111

12-
if (!isLogin())
12+
if (!accessToken)
1313
refresh()
1414
.then((res) => {
1515
setAuthInformation(res);

apps/client/src/routes/my.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ export const Route = createFileRoute('/my')({
1212
</React.Suspense>
1313
),
1414
beforeLoad: () => {
15-
const { isLogin, setAuthInformation } = useAuthStore.getState();
15+
const { accessToken, setAuthInformation } = useAuthStore.getState();
1616

17-
if (!isLogin()) {
17+
if (!accessToken) {
1818
return refresh()
1919
.then((res) => {
2020
setAuthInformation(res);

0 commit comments

Comments
 (0)