diff --git a/src/authorties.ts b/src/authorties.ts new file mode 100644 index 0000000..925445f --- /dev/null +++ b/src/authorties.ts @@ -0,0 +1,12 @@ + +export const [ + VIEW_USER, + MAINTAIN_USER, + VIEW_GROUP, + MAINTAIN_GROUP, + ] = [ + "VIEW_USER", + "MAINTAIN_USER", + "VIEW_GROUP", + "MAINTAIN_GROUP", +] \ No newline at end of file diff --git a/src/middleware.ts b/src/middleware.ts index f97f81e..4a01e15 100644 --- a/src/middleware.ts +++ b/src/middleware.ts @@ -1,21 +1,26 @@ import { NextRequestWithAuth, withAuth } from "next-auth/middleware"; import { authOptions } from "@/config/authConfig"; import { NextFetchEvent, NextResponse } from "next/server"; +import { PRIVATE_ROUTES } from "./routes"; -const PRIVATE_ROUTES = [ - "/analytics", - "/dashboard", - "/home", - "/invoice", - "/projects", - "/tasks", - "/settings", - "/material", -]; const LANG_QUERY_PARAM = "lang"; const authMiddleware = withAuth({ pages: authOptions.pages, + callbacks: { + authorized: ({ req, token }) => { + if (!Boolean(token)) { + return Boolean(token) + } + + // example + // const abilities = token!.abilities as string[] + // if (req.nextUrl.pathname.endsWith('/user') && 'abilities dont hv view/maintain user') { + // return false + // } + return true + } + } }); export default async function middleware( @@ -36,4 +41,4 @@ export default async function middleware( return PRIVATE_ROUTES.some((route) => req.nextUrl.pathname.startsWith(route)) ? await authMiddleware(req, event) // Let auth middleware handle response : NextResponse.next(); // Return normal response -} +} \ No newline at end of file diff --git a/src/routes.ts b/src/routes.ts new file mode 100644 index 0000000..7862f52 --- /dev/null +++ b/src/routes.ts @@ -0,0 +1,10 @@ +export const PRIVATE_ROUTES = [ + "/analytics", + "/dashboard", + "/home", + "/invoice", + "/projects", + "/tasks", + "/settings", + "/material", + ]; \ No newline at end of file