Initial commit
This commit is contained in:
36
proxy.ts
Normal file
36
proxy.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
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).*)"]
|
||||
};
|
||||
Reference in New Issue
Block a user