import { withAuth } from "next-auth/middleware"; import { NextResponse } from "next/server"; export const config = { matcher: [ /* * Match all paths except for: * 1. /api/ routes * 2. /_next/ (Next.js internals) * 3. /_proxy/ (special page for OG tags proxying) * 4. /_static (inside /public) * 5. /_vercel (Vercel internals) * 6. /favicon.ico, /sitemap.xml (static files) * 7. /auth page */ "/((?!api/|_next/|_proxy/|_static|_vercel|auth|terms|privacy-policy|not-found|avatars/|assets/|files/|admin/sourceMap/chrome/css/|admin/administration/sourceMap/chrome/css/|firebase-messaging-sw.js|sw.js|manifest.json|manifest.webmanifest|offline|favicon.ico|sitemap.xml).*)", ], // matcher: [ // "/", // "/admin/:path*", // // '/auth' ==> cause redirect panjang // ] }; export default withAuth( // `withAuth` augments your `Request` with the user's token. async function proxy(req) { const session: any = req.nextauth.token const path = req.nextUrl.pathname // console.log("URL", path) if (path === 'admin' || path.startsWith('/admin')) { if (!session.detail.role.allow_admin_panel) { // return NextResponse.redirect(new URL(basePath + '/', req.url)) return NextResponse.redirect(new URL('/', req.url)) } } if (path === '/users/sign_in') { return NextResponse.redirect(new URL('/', req.url)) } if (session?.error === 'RefreshAccessTokenError') { // return NextResponse.redirect(new URL(`/auth?callbackUrl=${req.url}&error=Your session has expired !`, req.url)) return NextResponse.redirect(new URL(`/auth?&error=Your session has expired !`, req.url)) } }, // { // pages: { // signIn: `/auth` //<-- i mean this, avoid basePath for defaultValue // }, // } );