Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/어드민페이지 url로 접근가능 #888 #893

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions src/components/NeedAuth/NeedAuth.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import React from 'react';
import { useNavigate } from 'react-router-dom';
import { Role } from '@api/dto';
import useCheckAuth from '@hooks/useCheckAuth';
import ConfirmModal from '@components/Modal/ConfirmModal';

interface NeedLoginProps {
children: JSX.Element;
roles?: Role[];
}

const NeedAuth = ({ children, roles = [] }: NeedLoginProps) => {
const { checkIncludeOneOfAuths } = useCheckAuth();
const navigate = useNavigate();

const onClose = () => {
navigate('/');
};

if (checkIncludeOneOfAuths([...roles, 'ROLE_회장', 'ROLE_부회장'])) {
return children;
}
return (
<ConfirmModal open onClose={onClose} title="권한이 필요한 서비스입니다">
<p>접근 권한이 없습니다</p>
</ConfirmModal>
);
};

export default NeedAuth;
28 changes: 28 additions & 0 deletions src/components/NeedAuth/NeedLogin.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import React from 'react';
import { useNavigate } from 'react-router-dom';
import useCheckAuth from '@hooks/useCheckAuth';
import ConfirmModal from '@components/Modal/ConfirmModal';

interface NeedLoginProps {
children: JSX.Element;
}

const NeedLogin = ({ children }: NeedLoginProps) => {
const { checkLogin } = useCheckAuth();
const navigate = useNavigate();

const onClose = () => {
navigate('/login');
};

if (checkLogin()) {
return children;
}
return (
<ConfirmModal open onClose={onClose} title="로그인이 필요한 서비스입니다">
<p>로그인 후 이용해주세요</p>
</ConfirmModal>
);
};

export default NeedLogin;
6 changes: 5 additions & 1 deletion src/hooks/useCheckAuth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ import memberState from '@recoil/member.recoil';
const useCheckAuth = () => {
const member: MemberInfo | null = useRecoilValue(memberState);

const checkLogin = useMemo(() => {
return () => member !== null;
}, [member]);

Comment on lines +9 to +12
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useMemo() 굿입니당!

const checkAuth = useMemo(() => {
return (requiredRole: Role) => member?.memberJobs?.includes(requiredRole);
}, [member]);
Expand All @@ -16,7 +20,7 @@ const useCheckAuth = () => {

const checkIsMyId = (id: number) => member?.memberId === id;

return { checkAuth, checkIncludeOneOfAuths, checkIsMyId };
return { checkLogin, checkAuth, checkIncludeOneOfAuths, checkIsMyId };
};

export default useCheckAuth;
86 changes: 72 additions & 14 deletions src/router/useMainRouter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ import SeminarAttend from '@pages/senimarAttend/SenimarAttend';
import FitContainer from '@components/Layout/Container/FitContainer';
import FullContainer from '@components/Layout/Container/FullContainer';
import MainLayout from '@components/Layout/MainLayout';
import NeedAuth from '@components/NeedAuth/NeedAuth';
import NeedLogin from '@components/NeedAuth/NeedLogin';

const useMainRouter = () =>
useRoutes([
Expand Down Expand Up @@ -54,7 +56,11 @@ const useMainRouter = () =>
},
{
path: 'profile/:memberId/*',
element: <Profile />,
element: (
<NeedLogin>
<Profile />
</NeedLogin>
),
},
],
},
Expand All @@ -66,27 +72,47 @@ const useMainRouter = () =>
children: [
{
path: 'dutyManage',
element: <DutyManage />,
element: (
<NeedAuth>
<DutyManage />
</NeedAuth>
),
},
/* {
path: 'electionManage',
element: <div />,
}, */
{
path: 'libraryManage/*',
element: <LibraryManage />,
element: (
<NeedAuth roles={['ROLE_사서']}>
<LibraryManage />
</NeedAuth>
),
},
{
path: 'seminarManage',
element: <SeminarManage />,
element: (
<NeedAuth roles={['ROLE_서기']}>
<SeminarManage />
</NeedAuth>
),
},
{
path: 'activeMemberManage',
element: <ActiveMemberManage />,
element: (
<NeedAuth roles={['ROLE_서기']}>
<ActiveMemberManage />
</NeedAuth>
),
},
{
path: 'meritManage',
element: <MeritManage />,
element: (
<NeedAuth roles={['ROLE_서기']}>
<MeritManage />
</NeedAuth>
),
},
],
},
Expand All @@ -95,41 +121,73 @@ const useMainRouter = () =>
children: [
{
path: ':categoryName',
element: <BoardList />,
element: (
<NeedLogin>
<BoardList />
</NeedLogin>
),
},
{
path: 'write/:categoryName',
element: <BoardWrite />,
element: (
<NeedLogin>
<BoardWrite />
</NeedLogin>
),
},
{
path: 'view/:postId',
element: <BoardView />,
element: (
<NeedLogin>
<BoardView />
</NeedLogin>
),
},
],
},
{
path: 'study',
element: <Study />,
element: (
<NeedLogin>
<Study />
</NeedLogin>
),
},
{
path: 'library',
element: <Library />,
element: (
<NeedLogin>
<Library />
</NeedLogin>
),
},
{
path: 'seminar',
element: <SeminarAttend />,
element: (
<NeedLogin>
<SeminarAttend />
</NeedLogin>
),
},
/* {
path: 'election',
element: <div />,
}, */
{
path: 'rank',
element: <Rank />,
element: (
<NeedLogin>
<Rank />
</NeedLogin>
),
},
{
path: 'game',
element: <Game />,
element: (
<NeedLogin>
<Game />
</NeedLogin>
),
},
/* {
path: 'ctf',
Expand Down
Loading