diff --git a/src/app/dashboard/coba/page.tsx b/src/app/dashboard/coba/page.tsx new file mode 100644 index 0000000..481b212 --- /dev/null +++ b/src/app/dashboard/coba/page.tsx @@ -0,0 +1,7 @@ +import React from 'react' + +export default function Page() { + return ( +
page
+ ) +} diff --git a/src/app/dashboard/page.tsx b/src/app/dashboard/page.tsx index f58e804..be12964 100644 --- a/src/app/dashboard/page.tsx +++ b/src/app/dashboard/page.tsx @@ -1,6 +1,12 @@ +import checkPermission from '@/modules/auth/utils/checkPermission' +import dashboardConfig from '@/modules/dashboard/dashboard.config' +import { redirect } from 'next/navigation' import React from 'react' export default async function Dashboard() { + + if (!await checkPermission("authenticated-only")) redirect(`${dashboardConfig.baseRoute}/login`) + return (

Dashboard

diff --git a/src/app/dashboard/(auth)/permissions/page.tsx b/src/app/dashboard/permissions/page.tsx similarity index 100% rename from src/app/dashboard/(auth)/permissions/page.tsx rename to src/app/dashboard/permissions/page.tsx diff --git a/src/app/dashboard/(auth)/roles/page.tsx b/src/app/dashboard/roles/page.tsx similarity index 100% rename from src/app/dashboard/(auth)/roles/page.tsx rename to src/app/dashboard/roles/page.tsx diff --git a/src/app/dashboard/(auth)/users/page.tsx b/src/app/dashboard/users/page.tsx similarity index 100% rename from src/app/dashboard/(auth)/users/page.tsx rename to src/app/dashboard/users/page.tsx diff --git a/src/app/page.tsx b/src/app/page.tsx index cb2be10..bc1f838 100644 --- a/src/app/page.tsx +++ b/src/app/page.tsx @@ -1,7 +1,12 @@ +import { redirect } from 'next/navigation' import React from 'react' export default function page() { + + redirect("/dashboard") + return (
page
) } + \ No newline at end of file diff --git a/src/modules/auth/actions/getMyDetailAction.ts b/src/modules/auth/actions/getMyDetailAction.ts index 66ef024..19213b1 100644 --- a/src/modules/auth/actions/getMyDetailAction.ts +++ b/src/modules/auth/actions/getMyDetailAction.ts @@ -35,10 +35,11 @@ export default async function getMyDetailAction(): Promise { setUser(response.data); }) .catch((error) => { + if (error instanceof ClientError){ + if (error.errorCode === "UNAUTHENTICATED") return; + } notifications.show({ title: 'Error', message: 'Error while retrieving user data', diff --git a/src/modules/dashboard/components/DashboardLayout.tsx b/src/modules/dashboard/components/DashboardLayout.tsx index 40b5745..3441486 100644 --- a/src/modules/dashboard/components/DashboardLayout.tsx +++ b/src/modules/dashboard/components/DashboardLayout.tsx @@ -1,11 +1,13 @@ "use client"; -import React, { useEffect } from "react"; +import React, { useEffect, useState } from "react"; import { AppShell } from "@mantine/core"; import { useDisclosure } from "@mantine/hooks"; import AppHeader from "./AppHeader"; import AppNavbar from "./AppNavbar"; import { useAuth } from "@/modules/auth/contexts/AuthContext"; +import { usePathname } from "next/navigation"; +import dashboardConfig from "../dashboard.config"; interface Props { children: React.ReactNode; @@ -20,16 +22,25 @@ interface Props { * @returns A React element representing the dashboard layout. */ export default function DashboardLayout(props: Props) { + + const pathname = usePathname(); + // State and toggle function for handling the disclosure of the navigation bar const [openNavbar, { toggle }] = useDisclosure(false); const {fetchUserData} = useAuth(); + const [withAppShell, setWithAppShell] = useState(false) + useEffect(() => { fetchUserData() }, [fetchUserData]) - return ( + useEffect(() => { + setWithAppShell(!dashboardConfig.routesWithoutAppShell.some(v => `${dashboardConfig.baseRoute}${v}` === pathname)) + }, [pathname]) + + return withAppShell ? ( - ); + ) : props.children; } diff --git a/src/modules/dashboard/dashboard.config.ts b/src/modules/dashboard/dashboard.config.ts new file mode 100644 index 0000000..5e1fad0 --- /dev/null +++ b/src/modules/dashboard/dashboard.config.ts @@ -0,0 +1,9 @@ +const dashboardConfig = { + routesWithoutAppShell : [ + "/login", + "/register", + ], + baseRoute: "/dashboard" +} as const; + +export default dashboardConfig; diff --git a/src/modules/dashboard/utils/withServerAction.ts b/src/modules/dashboard/utils/withServerAction.ts index 52d1550..4964f61 100644 --- a/src/modules/dashboard/utils/withServerAction.ts +++ b/src/modules/dashboard/utils/withServerAction.ts @@ -1,5 +1,6 @@ import ClientError from "@/core/error/ClientError"; import ServerResponseAction from "../types/ServerResponseAction"; +import { error } from "console"; /** * A higher-order function that wraps an async function and provides structured error handling.