32 lines
980 B
TypeScript
32 lines
980 B
TypeScript
// middleware.ts
|
|
import { NextResponse } from "next/server";
|
|
import type { NextRequest } from "next/server";
|
|
|
|
export function middleware(req: NextRequest) {
|
|
const { pathname, searchParams, origin } = req.nextUrl;
|
|
|
|
const isAuthPage =
|
|
pathname === "/auth/sign-in" || pathname === "/auth/sign-up";
|
|
const isMyArea = pathname.startsWith("/my/");
|
|
|
|
const at = req.cookies.get("ma_at")?.value;
|
|
|
|
// Gate /my/*
|
|
if (isMyArea && !at) {
|
|
const dest = new URL("/auth/sign-in", origin);
|
|
dest.searchParams.set("next", pathname + (req.nextUrl.search || ""));
|
|
return NextResponse.redirect(dest);
|
|
}
|
|
|
|
// If logged in and on auth pages, send to next or /my/rigs
|
|
if (isAuthPage && at) {
|
|
const nxt = searchParams.get("next") || "/my/rigs";
|
|
return NextResponse.redirect(new URL(nxt, origin));
|
|
}
|
|
|
|
return NextResponse.next();
|
|
}
|
|
|
|
export const config = {
|
|
matcher: ["/my/:path*", "/auth/sign-in", "/auth/sign-up"],
|
|
};
|