add crm auth, email, status update and search
All checks were successful
Auto Deploy / deploy (push) Successful in 1m7s

This commit is contained in:
deonisii
2026-04-17 21:29:14 +03:00
parent 246fb6d52d
commit 4f67bca4be
16 changed files with 502 additions and 35 deletions

41
middleware.ts Normal file
View File

@@ -0,0 +1,41 @@
import { NextRequest, NextResponse } from "next/server";
import { getSessionCookieName, verifySessionToken } from "@/lib/auth";
export async function middleware(request: NextRequest) {
const { pathname, search } = request.nextUrl;
const host = request.headers.get("host") || "";
const crmHost = process.env.CRM_HOST || "crm.workparking.ru";
if (!pathname.startsWith("/admin")) {
return NextResponse.next();
}
if (host !== crmHost) {
const redirectUrl = new URL(request.url);
redirectUrl.host = crmHost;
redirectUrl.protocol = "https:";
return NextResponse.redirect(redirectUrl);
}
const cookieName = getSessionCookieName();
const token = request.cookies.get(cookieName)?.value;
const isAuthed = await verifySessionToken(token);
const isLoginPage = pathname === "/admin/login";
if (!isAuthed && !isLoginPage) {
const loginUrl = new URL("/admin/login", request.url);
loginUrl.searchParams.set("next", `${pathname}${search}`);
return NextResponse.redirect(loginUrl);
}
if (isAuthed && isLoginPage) {
return NextResponse.redirect(new URL("/admin/leads", request.url));
}
return NextResponse.next();
}
export const config = {
matcher: ["/admin/:path*"],
};