|
|
@@ -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 |
|
|
|
} |
|
|
|
} |