Skip to content

Commit

Permalink
[Refactor] : 어드민 서비스 미들웨어 로직 추가 및 스터디 리스트 레이아웃 조정 (#94)
Browse files Browse the repository at this point in the history
* chore: middleware 캐시 로직 추가

* fix: 오타 수정

* fix: 커리큘럼을 스터디로 변경해요

* feat: Navbar 클릭하면 메인페이지로 이동

* fix: 절대경로 변경

* fix: 스터디 상세 레이아웃 수정

* fix: url constants로 분리

* fix: error처리 삭제, 이름 변경

* fix: 어드민 전용으로 쿠키 이름 변경

* fix: 만료 시간 제거
  • Loading branch information
eugene028 authored Sep 2, 2024
1 parent 83eac0f commit 7d42c20
Show file tree
Hide file tree
Showing 10 changed files with 64 additions and 24 deletions.
2 changes: 1 addition & 1 deletion apps/admin/app/studies/[studyId]/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ const StudyLayout = ({
};

const MainLayoutStyle = {
height: "100vh",
height: "100%",
overflow: "auto",
};

Expand Down
17 changes: 14 additions & 3 deletions apps/admin/app/studies/_components/StudyListItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ const StudyListItem = async ({ study }: { study: StudyListApiResponseDto }) => {
{academicYear}-{semesterType === "FIRST" ? "1" : "2"}
</Text>
<Flex alignItems="center" gap="xs">
<Text typo="h3">{title}</Text>
<Text style={StudyNameStyle} typo="h3">
{title}
</Text>
<Tag color={studyTypeColorMap[studyType]} variant="solid1">
{studyType}
</Tag>
Expand Down Expand Up @@ -70,8 +72,8 @@ const studyTypeColorMap: Record<
ComponentProps<typeof Tag>["color"]
> = {
"과제 스터디": "green",
"온라인 커리큘럼": "blue",
"오프라인 커리큘럼": "yellow",
"온라인 스터디": "blue",
"오프라인 스터디": "yellow",
};

const LinkStyle = {
Expand All @@ -83,12 +85,21 @@ const LinkStyle = {

const TableLeftStyle = {
display: "flex",
flex: 2,
alignItems: "center",
gap: "31px",
};

const TableRightStyle = {
display: "flex",
flex: 3,
alignItems: "center",
gap: "64px",
};

const StudyNameStyle = {
whiteSpace: "nowrap",
overflow: "hidden",
textOverflow: "ellipsis",
maxWidth: "150px",
};
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const StudyFormatSelect = () => {
value="OFFLINE"
text={
<Flex alignItems="center" gap="md">
<Text typo="body1">오프라인 커리큘럼</Text>
<Text typo="body1">오프라인 스터디</Text>
<Text color="sub" typo="body2">
오프라인으로 진행해요.
</Text>
Expand All @@ -34,7 +34,7 @@ const StudyFormatSelect = () => {
value="ONLINE"
text={
<Flex alignItems="center" gap="md">
<Text typo="body1">온라인 커리큘럼</Text>
<Text typo="body1">온라인 스터디</Text>
<Text color="sub" typo="body2">
온라인으로 진행해요.
</Text>
Expand Down
4 changes: 4 additions & 0 deletions apps/admin/constants/cookieKey.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const enum cookieKey {
accessToken = "accessToken",
"admin-middleware-executed" = "admin-middleware-executed",
}
4 changes: 4 additions & 0 deletions apps/admin/constants/url.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const clientUrl =
process.env.NEXT_PUBLIC_VERCEL_ENV === "production"
? process.env.NEXT_PUBLIC_CLIENT_PROD_URL
: process.env.NEXT_PUBLIC_CLIENT_DEV_URL;
3 changes: 3 additions & 0 deletions apps/admin/hooks/useForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const useForm = () => {};

export default useForm;
39 changes: 25 additions & 14 deletions apps/admin/middleware.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,40 @@
import { dashboardApi } from "apis/auth/dashboardApi";
import { cookieKey } from "constants/cookieKey";
import { clientUrl } from "constants/url";
import { cookies } from "next/headers";
import type { NextRequest } from "next/server";
import { NextResponse } from "next/server";

import setExpireTime from "utils/setExpireTime";
export const config = {
matcher: ["/studies/:path*", "/participants/:path*"],
};

const middleware = async (req: NextRequest) => {
const middleware = async () => {
const cookieStore = cookies();
const accessToken = cookieStore.get("accessToken")?.value;
const accessToken = cookieStore.get(cookieKey.accessToken)?.value;
const middlewareExecuted = cookieStore.get(
cookieKey["admin-middleware-executed"]
)?.value;

if (!accessToken) {
return NextResponse.redirect(new URL("/not-found", req.url));
return NextResponse.redirect(new URL("/auth", clientUrl));
}

const { studyRole, manageRole } = await dashboardApi.getDashboardInfo();

if (studyRole === "STUDENT" && manageRole === "NONE") {
const url =
process.env.NEXT_PUBLIC_VERCEL_ENV === "production"
? process.env.NEXT_PUBLIC_CLIENT_PROD_URL
: process.env.NEXT_PUBLIC_CLIENT_DEV_URL;

return NextResponse.redirect(new URL("/auth", url));
if (!middlewareExecuted) {
try {
const { manageRole, studyRole } = await dashboardApi.getDashboardInfo();
if (studyRole === "STUDENT" && manageRole === "NONE") {
return NextResponse.redirect(new URL("/auth", clientUrl));
}
const response = NextResponse.next();
response.cookies.set(cookieKey["admin-middleware-executed"], "true", {
httpOnly: true,
secure: true,
sameSite: "lax",
});
return response;
} catch (error) {
return NextResponse.next();
}
}

return NextResponse.next();
Expand Down
4 changes: 2 additions & 2 deletions apps/admin/types/entities/study.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export type StudyType = "ASSIGNMENT" | "ONLINE" | "OFFLINE";

export type StudyKoreanType =
| "과제 스터디"
| "온라인 커리큘럼"
| "오프라인 커리큘럼";
| "온라인 스터디"
| "오프라인 스터디";

export type StudyCurriculumType = {
studyDetailId: number;
Expand Down
8 changes: 8 additions & 0 deletions apps/admin/utils/setExpireTime.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
const setExpireTime = (hour: number) => {
const expires = new Date();
expires.setTime(expires.getTime() + hour * 60 * 60 * 1000);

return expires;
};

export default setExpireTime;
3 changes: 1 addition & 2 deletions packages/ui/src/components/NavItem/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ const NavItem = ({ href, imageUrl, alt, name, items }: NavItemProps) => {
href={`${href}`}
tabIndex={0}
className={navItemStyle({
type:
!segment[1] && `/${segment[0]}` === href ? "active" : "inactive",
type: !segment[1] && `${segment[0]}` === href ? "active" : "inactive",
})}
onClick={handleClickNavItem}
>
Expand Down

0 comments on commit 7d42c20

Please sign in to comment.