assessment-model/src/middleware.ts
Khalim Conn-Kowlessar dce30edf00 fixed filtering logic
2026-02-07 19:59:51 +00:00

49 lines
1.3 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
import { getToken } from "next-auth/jwt";
export async function middleware(req: NextRequest) {
const token = await getToken({ req });
const { pathname } = req.nextUrl;
// If no session, send user to sign-in page
if (!token) {
return NextResponse.redirect(new URL("/", req.url));
}
const userEmail = token.email || "";
// Internal users (bypass onboarding)
const isInternal = userEmail.endsWith("@domna.homes");
// Not onboarded and not internal
if (token.onboarded === false && pathname !== "/onboarding" && !isInternal) {
return NextResponse.redirect(new URL("/onboarding", req.url));
}
// Already onboarded but tries to go back to onboarding page
if (token.onboarded === true && pathname === "/onboarding") {
return NextResponse.redirect(new URL("/home", req.url));
}
// If internal, allow access to everything
if (isInternal) {
return NextResponse.next();
}
// Everything else allowed
return NextResponse.next();
}
export const config = {
matcher: [
// Protect only apps authenticated areas
"/home/:path*",
"/portfolio/:path*",
"/search/:path*",
"/addresses/:path*",
"/due-considerations/:path*",
"/eco-spreadsheet/:path*",
"/onboarding", // add onboarding itself
],
};