37 lines
1.0 KiB
TypeScript
37 lines
1.0 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { getToken } from "next-auth/jwt";
|
|
|
|
const AUTH_ROUTES = ["/login", "/register"];
|
|
|
|
export async function proxy(request: NextRequest) {
|
|
const pathname = request.nextUrl.pathname;
|
|
|
|
const atMatch = pathname.match(/^\/@([a-zA-Z0-9_]{3,32})$/);
|
|
if (atMatch) {
|
|
const url = request.nextUrl.clone();
|
|
url.pathname = `/u/${atMatch[1].toLowerCase()}`;
|
|
return NextResponse.rewrite(url);
|
|
}
|
|
|
|
const token = await getToken({ req: request });
|
|
|
|
if (pathname.startsWith("/dashboard") && !token) {
|
|
const url = request.nextUrl.clone();
|
|
url.pathname = "/login";
|
|
url.searchParams.set("next", pathname);
|
|
return NextResponse.redirect(url);
|
|
}
|
|
|
|
if (AUTH_ROUTES.includes(pathname) && token) {
|
|
const url = request.nextUrl.clone();
|
|
url.pathname = "/dashboard";
|
|
return NextResponse.redirect(url);
|
|
}
|
|
|
|
return NextResponse.next();
|
|
}
|
|
|
|
export const config = {
|
|
matcher: ["/((?!api|_next/static|_next/image|favicon.ico|robots.txt|sitemap.xml).*)"]
|
|
};
|