From 55a02f0e8bc5c8a9a9088ee1744d28093b57db54 Mon Sep 17 00:00:00 2001 From: William Tanardi Date: Sat, 27 May 2023 00:17:48 +0700 Subject: [PATCH] added dashboard routes protection --- src/app/dashboard/admin/page.tsx | 10 +++++++++- src/app/dashboard/user/page.tsx | 5 ++--- 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/src/app/dashboard/admin/page.tsx b/src/app/dashboard/admin/page.tsx index e11523d..94733bc 100644 --- a/src/app/dashboard/admin/page.tsx +++ b/src/app/dashboard/admin/page.tsx @@ -12,14 +12,22 @@ import RecipeAdd from "./RecipeAdd"; import OrderAdd from "./OrderAdd"; import IngredientAdd from "./IngredientAdd"; import { LogoutButton } from "@/components/Buttons"; +import { getServerSession } from "next-auth"; +import { redirect } from "next/navigation"; -const AdminDashboard = () => { +const AdminDashboard = async () => { const [activeTab, setActiveTab] = useState("tab1"); const handleTabChange = (tabName: string) => { setActiveTab(tabName); }; + const session = await getServerSession(); + + if (!session) { + redirect("/login"); + } + return ( <>
diff --git a/src/app/dashboard/user/page.tsx b/src/app/dashboard/user/page.tsx index cb95a37..a152638 100644 --- a/src/app/dashboard/user/page.tsx +++ b/src/app/dashboard/user/page.tsx @@ -13,7 +13,6 @@ import { import "@fortawesome/fontawesome-svg-core/styles.css"; import "../../../../fontawesome"; import { getServerSession } from "next-auth"; -import { authOptions } from "@/lib/auth"; import { redirect } from "next/navigation"; import { PrismaClient } from "@prisma/client"; import { LogoutButton } from "@/components/Buttons"; @@ -55,10 +54,10 @@ const getRecipes = async () => { }; const UserDashboard = async () => { - const session = await getServerSession(authOptions); + const session = await getServerSession(); if (!session) { - redirect("/api/auth/signin"); + redirect("/login"); } const [ingredients, categories, recipes] = await Promise.all([