56 lines
1.3 KiB
TypeScript
56 lines
1.3 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import type { NextRequest } from "next/server";
|
|
|
|
const AUTH_SESSION_COOKIE = "boss_session";
|
|
|
|
function hasSessionCookie(request: NextRequest) {
|
|
return Boolean(request.cookies.get(AUTH_SESSION_COOKIE)?.value);
|
|
}
|
|
|
|
function redirectTo(pathname: string, request: NextRequest) {
|
|
return NextResponse.redirect(new URL(pathname, request.url));
|
|
}
|
|
|
|
export function proxy(request: NextRequest) {
|
|
const pathname = request.nextUrl.pathname;
|
|
const authenticated = hasSessionCookie(request);
|
|
|
|
if (pathname === "/") {
|
|
return redirectTo(authenticated ? "/conversations" : "/auth/login", request);
|
|
}
|
|
|
|
if (
|
|
(pathname === "/auth/login" ||
|
|
pathname === "/auth/register" ||
|
|
pathname === "/auth/forgot-password") &&
|
|
authenticated
|
|
) {
|
|
return redirectTo("/conversations", request);
|
|
}
|
|
|
|
if (
|
|
(pathname.startsWith("/conversations") ||
|
|
pathname.startsWith("/devices") ||
|
|
pathname.startsWith("/me") ||
|
|
pathname.startsWith("/threads")) &&
|
|
!authenticated
|
|
) {
|
|
return redirectTo("/auth/login", request);
|
|
}
|
|
|
|
return NextResponse.next();
|
|
}
|
|
|
|
export const config = {
|
|
matcher: [
|
|
"/",
|
|
"/auth/login",
|
|
"/auth/register",
|
|
"/auth/forgot-password",
|
|
"/conversations/:path*",
|
|
"/devices/:path*",
|
|
"/me/:path*",
|
|
"/threads/:path*",
|
|
],
|
|
};
|