Skip to content

Commit

Permalink
[FEAT] 페이지 접근 인증 모듈 추가 (#123)
Browse files Browse the repository at this point in the history
* feat: add auth module

* feat: add auth module to admin layout
  • Loading branch information
hynseok authored Jan 15, 2025
1 parent 3648de9 commit da5bfcd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/app/(admin)/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { Metadata } from "next";
import classes from "./layout.module.css";
import { AdminSidebar } from "@/components/pages/AdminSidebar";
import { AuthModule } from "@/components/common/Auth/AuthModule";

export const metadata: Metadata = {
title: "S-TOP 기술교류회 관리자",
description: "S-TOP 기술교류회 관리자",
};

export default function AdminLayout({ children }: { children: React.ReactNode }) {
export default async function AdminLayout({ children }: { children: React.ReactNode }) {
await AuthModule({ userType: ["ADMIN"] });

return (
<section className={classes.container}>
<AdminSidebar />
Expand Down
30 changes: 30 additions & 0 deletions src/components/common/Auth/AuthModule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
"use server";

import { JWT_COOKIE_NAME } from "@/constants/Auth";
import { redirect } from "next/navigation";
import { cookies } from "next/headers";
import { jwtDecode, JwtPayload } from "jwt-decode";
import { Role } from "@/types/user";

export interface CustomJwtPayload extends JwtPayload {
userType: Role;
}

interface Props {
userType: Role[];
}

export async function AuthModule({ userType }: Props): Promise<{ token: string }> {
const token = cookies().get(JWT_COOKIE_NAME)?.value;

if (!token) {
redirect("/login");
}

const decoded = jwtDecode<CustomJwtPayload>(token);
if (!userType.includes(decoded.userType)) {
redirect("/");
}

return { token };
}

0 comments on commit da5bfcd

Please sign in to comment.