const publicPaths = ["/api/auth", "/api/_status", "/static", "favicon.ico"];
export default async function middleware(req: NextRequest) {
const url = req.nextUrl.clone();
// allow to unauthorized access for these paths
if (publicPaths.some((path) => url.pathname.includes(path)))
return NextResponse.next();
// fetch here requires an absolute URL to the auth API route
const {
data: { auth },
} = await fetch(
`${url.origin}${env.NEXT_PUBLIC_APP_BASE_PATH}/api/auth/authed`,
{
headers: req.headers,
}
).then((res) => res.json());
// we patch the callback to send the user back to where auth was required
url.search = new URLSearchParams(`callbackUrl=${url}`).toString();
url.pathname = `/api/auth/signin`;
return !auth ? NextResponse.redirect(url) : NextResponse.next();
}
const publicPaths = ["/api/auth", "/api/_status", "/static", "favicon.ico"];
export default async function middleware(req: NextRequest) {
const url = req.nextUrl.clone();
// allow to unauthorized access for these paths
if (publicPaths.some((path) => url.pathname.includes(path)))
return NextResponse.next();
// fetch here requires an absolute URL to the auth API route
const {
data: { auth },
} = await fetch(
`${url.origin}${env.NEXT_PUBLIC_APP_BASE_PATH}/api/auth/authed`,
{
headers: req.headers,
}
).then((res) => res.json());
// we patch the callback to send the user back to where auth was required
url.search = new URLSearchParams(`callbackUrl=${url}`).toString();
url.pathname = `/api/auth/signin`;
return !auth ? NextResponse.redirect(url) : NextResponse.next();
}