Skip to content

Commit

Permalink
Fix: 인증 플로우 관련 개선
Browse files Browse the repository at this point in the history
  • Loading branch information
sjsjsj1246 committed Oct 10, 2023
1 parent 50dab43 commit b1c9264
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 36 deletions.
3 changes: 3 additions & 0 deletions src/components/Layouts/AuthzLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ function AuthzLayout() {
} = useAuth();

useEffect(() => {
if (!isAuth) {
location.replace(ROUTE.LOGIN);
}
if (accountMe && accountMe.status !== 'LINKED' && accountMe.status !== 'DEMO') {
location.replace(ROUTE.REGISTER);
}
Expand Down
13 changes: 5 additions & 8 deletions src/hooks/auth/useAuth/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { useMutation, useQuery } from '@tanstack/react-query';
import { demoStart, getAccountMe } from '@libs/api/auth';
import { useNavigate } from 'react-router';
import ROUTE from '@libs/constant/path';
import useInitStore from '@hooks/useResetStore';
import useInitStore from '@hooks/useInitStore';

const useAuth = () => {
const [isAuth, accessToken, nurseId, accountId, wardId, demoStartDate, setState, initState] =
const [isAuth, accessToken, nurseId, accountId, wardId, demoStartDate, _loaded, setState] =
useAuthStore(
(state) => [
state.isAuth,
Expand All @@ -17,8 +17,8 @@ const useAuth = () => {
state.accountId,
state.wardId,
state.demoStartDate,
state._loaded,
state.setState,
state.initState,
],
shallow
);
Expand All @@ -34,18 +34,14 @@ const useAuth = () => {
setState('accountId', account.accountId);
setState('nurseId', account.nurseId);
},
onError: () => {
initState();
},
enabled: accessToken !== null,
enabled: !!_loaded,
});

const handleLogout = () => {
initStore();
};

const handleLogin = (accessToken: string) => {
initStore();
axiosInstance.defaults.headers.common['Authorization'] = `Bearer ${accessToken}`;
setState('accessToken', accessToken);
};
Expand Down Expand Up @@ -75,6 +71,7 @@ const useAuth = () => {
accountId,
wardId,
demoStartDate,
_loaded,
},
actions: {
handleLogin,
Expand Down
32 changes: 16 additions & 16 deletions src/hooks/auth/useAuth/store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import { create } from 'zustand';
import { devtools, persist } from 'zustand/middleware';
import { produce } from 'immer';
import { setAccessToken } from '@libs/api/client';

interface State {
isAuth: boolean;
Expand All @@ -10,6 +11,7 @@ interface State {
nurseId: number | null;
wardId: number | null;
demoStartDate: string | null;
_loaded: boolean;
}

interface Store extends State {
Expand All @@ -24,6 +26,7 @@ const initialState: State = {
nurseId: null,
wardId: null,
demoStartDate: null,
_loaded: false,
};

const useAuthStore = create<Store>()(
Expand All @@ -37,25 +40,22 @@ const useAuthStore = create<Store>()(
draft[key] = value;
})
),
initState: () => set(initialState),
initState: () => set({ ...initialState, _loaded: true }),
}),
{
name: 'useAuthStore',
partialize: ({
isAuth,
accessToken,
accountId,
nurseId,
wardId,
demoStartDate,
}: Store) => ({
isAuth,
accessToken,
accountId,
nurseId,
wardId,
demoStartDate,
}),
partialize: ({ isAuth, accessToken, accountId, nurseId, wardId, demoStartDate }: Store) => {
if (accessToken) setAccessToken(accessToken);
return {
isAuth,
accessToken,
accountId,
nurseId,
wardId,
demoStartDate,
_loaded: true,
};
},
}
)
)
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/libs/api/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ axiosInstance.interceptors.response.use(
toast.error('에러가 발생했습니다. 다시 시도해주세요.');
});

return Promise.reject(error);
return Promise.reject({ code: error.response.status, message: error.response.data.message });
}
);

Expand Down
2 changes: 1 addition & 1 deletion src/pages/MakeShiftPage/components/Toolbar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ function Toolbar() {
onClick={() => changeMonth('next')}
className="h-[1.875rem] w-[1.875rem] cursor-pointer"
/>
<p className="ml-[1.25rem] font-apple text-[.875rem] text-main-1">
<p className="ml-[1.25rem] font-apple text-[.875rem] text-main-1" onClick={() => test()}>
기본 OFF {shift?.days.filter((x) => x.dayType !== 'workday').length}
</p>
</div>
Expand Down
20 changes: 10 additions & 10 deletions src/pages/RegisterPage/RegisterWard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,6 @@ const schema = yup.object().shape({
function RegisterWard() {
const [shiftTeams, setShiftTeams] = useState<string[][]>([[]]);
const [wardShiftTypes, setWardShiftTypes] = useState([
{
name: '오프',
shortName: 'O',
startTime: '',
endTime: '',
backgroundColor: '#465B7A',
textColor: '#FFFFFF',
isDefault: true,
isOff: true,
},
{
name: '데이',
shortName: 'D',
Expand Down Expand Up @@ -75,6 +65,16 @@ function RegisterWard() {
isDefault: true,
isOff: false,
},
{
name: '오프',
shortName: 'O',
startTime: '',
endTime: '',
backgroundColor: '#465B7A',
textColor: '#FFFFFF',
isDefault: true,
isOff: true,
},
]);
const {
formState: { errors, isValid },
Expand Down

0 comments on commit b1c9264

Please sign in to comment.