Skip to content

Commit

Permalink
added middleware for admin page
Browse files Browse the repository at this point in the history
  • Loading branch information
joywin2003 committed Sep 13, 2024
1 parent 5167c4a commit d4f9d82
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/app/admin/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import React from 'react'

export default function page() {
return (
<div>Admin Page</div>
)
}
21 changes: 21 additions & 0 deletions src/middleware.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export { default } from "next-auth/middleware";
import { getToken } from "next-auth/jwt";

// This function can be marked `async` if using `await` inside
export async function middleware(request: NextRequest) {
const token = await getToken({ req: request });
const url = request.nextUrl;

if (url.pathname === "/admin") {
if (token?.role!=="ADMIN") {
return NextResponse.redirect(new URL("/", request.url));
}
}
}

// See "Matching Paths" below to learn more
export const config = {
matcher: ["/:path*", "/"],
};

0 comments on commit d4f9d82

Please sign in to comment.