Skip to content

Commit

Permalink
feat: 사용자 타입에 맞지 않는 마이페이지 접근 제한 기능 추가
Browse files Browse the repository at this point in the history
  • Loading branch information
KimJi-An committed Nov 9, 2024
1 parent b14d969 commit a51dcbc
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
22 changes: 22 additions & 0 deletions src/routes/guards/RequireAuth.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { userLocalStorage } from '@/utils/storage';
import { useNavigate } from 'react-router-dom';
import ROUTE_PATH from '../path';
import { ReactNode, useEffect } from 'react';

interface RequireAuthProps {
children: ReactNode;
expectedType: 'employee' | 'employer';
}

export function RequireAuth({ children, expectedType }: RequireAuthProps) {
const user = userLocalStorage.getUser();
const navigate = useNavigate();

useEffect(() => {
if (!user || user.type !== expectedType) {
navigate(ROUTE_PATH.HOME);
}
}, [user, expectedType, navigate]);

return user && user.type === expectedType ? children : null;
}
19 changes: 17 additions & 2 deletions src/routes/router.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import EmployeeContract from '@/pages/contract/EmployeeContract/EmployeeContract
import EmployerContract from '@/pages/contract/EmployerContract/EmployerContract';
import MyCompanyPage from '@/pages/myCompanyPage/MyCompanyPage';
import ApplicantsPage from '@/pages/applicantsPage/ApplicantsPage';
import { RequireAuth } from './guards/RequireAuth';

export const router = createBrowserRouter([
{
Expand All @@ -41,12 +42,26 @@ export const router = createBrowserRouter([
{ path: ROUTE_PATH.APPLY.APPLYPAGE, element: <ApplyPage /> },
{ path: ROUTE_PATH.RECRUIT, element: <Recruit /> },
{ path: ROUTE_PATH.REGISTER_VISA, element: <RegisterVisa /> },
{ path: ROUTE_PATH.EMPLOYEE.EMPLOYEE_PAGE, element: <EmployeeMyPage /> },
{
path: ROUTE_PATH.EMPLOYEE.EMPLOYEE_PAGE,
element: (
<RequireAuth expectedType="employee">
<EmployeeMyPage />
</RequireAuth>
),
},
{ path: ROUTE_PATH.POST_NOTICE, element: <PostNotice /> },
{ path: ROUTE_PATH.MY_COMPANY, element: <MyCompanyPage /> },
{ path: ROUTE_PATH.APPLICANTS, element: <ApplicantsPage /> },
{ path: ROUTE_PATH.RESUME, element: <Resume /> },
{ path: ROUTE_PATH.MY_PAGE.EMPLOYER, element: <EmployerMyPage /> },
{
path: ROUTE_PATH.MY_PAGE.EMPLOYER,
element: (
<RequireAuth expectedType="employer">
<EmployerMyPage />
</RequireAuth>
),
},
{ path: ROUTE_PATH.REGISTERSIGN, element: <RegisterSign /> },
{ path: ROUTE_PATH.REGISTERCOMPANY, element: <RegisterCompany /> },
{ path: ROUTE_PATH.CONTRACT.EMPLOYEE, element: <EmployeeContract /> },
Expand Down

0 comments on commit a51dcbc

Please sign in to comment.