echos-ocr/src/middleware.ts

109 lines
2.4 KiB
TypeScript
Raw Normal View History

import { NextRequest, NextResponse } from "next/server";
import { getToken } from "next-auth/jwt";
const publicPaths = [
"/welcome",
"/features",
"/pricing",
"/privacy",
"/terms",
"/login",
"/signup",
"/invite",
"/forgot-password",
"/reset-password",
"/setup",
"/s",
"/api/auth",
"/api/health",
"/api/setup",
"/api/onboarding",
"/api/invitations/verify",
"/api/survey/submit",
"/api/jobs/process",
"/api/email-watch/poll",
"/api/ftp-watch/poll",
];
const workspaceSetupExemptPaths = [
"/workspace-setup",
"/onboarding",
"/api/onboarding",
"/api/org/create-personal",
"/api/org/list",
"/api/auth",
];
const onboardingExemptPaths = [
"/onboarding",
"/api/onboarding",
"/api/auth",
];
function isPublic(pathname: string) {
return publicPaths.some(
(p) => pathname === p || pathname.startsWith(p + "/")
);
}
function isOnboardingExempt(pathname: string) {
return onboardingExemptPaths.some(
(p) => pathname === p || pathname.startsWith(p + "/")
);
}
export async function middleware(req: NextRequest) {
const { pathname } = req.nextUrl;
Add dynamic fields, people directory, analytics, security hardening, and UX polish Phase 1 - Security & Bug Fixes: - Add requireApiAuth helper and protect all 25 unprotected API routes - Add org-tenant scoping to all card, job, stats, and notification queries - Fix SSRF in ai-test, mask secrets in settings API, fix middleware bypass - Fix cards pagination routing, stat filter sync, drag-drop file passing - Add PUT /api/auth/me for profile persistence, stuck job recovery - Fix email watcher MIME type detection Phase 2 - Dynamic Fields & Digital Survey: - Add FormTemplate, FormField, Person, PasswordResetToken models to schema - Add fieldData, formTemplateId, firstName, lastName, personId to ResponseCard - Build FormTemplate CRUD API with field management and org scoping - Build Form Builder UI with field ordering, type config, and section management - Refactor card detail page to render fields dynamically from templates - Add dynamic OCR prompt/schema generation from template fields - Build public survey page at /s/[orgSlug]/[formSlug] with branding - Add QR code generation API and share section component Phase 3 - People & Analytics: - Build People CRUD API with merge and batch auto-link endpoints - Build People list and detail pages with search, merge dialog - Add auto-link logic in OCR completion to match/create Person records - Add /api/stats/trends endpoint with time series and team activity - Build Reports page with Recharts (area charts, bar charts, pipeline) - Upgrade dashboard with sparklines and People stat card Phase 4 - UX Polish: - Replace silent error handling with toast notifications across all pages - Add loading skeletons, differentiated empty states - Add ARIA labels, skip-to-content link, accessible column toggle - Add forgot password flow, Cmd+K command palette, Collection Days pages - Unify Echo branding and theme toggle consistency Made-with: Cursor
2026-04-17 00:29:26 -04:00
const isStaticFile = pathname.startsWith("/_next/") ||
pathname.startsWith("/favicon") ||
Add dynamic fields, people directory, analytics, security hardening, and UX polish Phase 1 - Security & Bug Fixes: - Add requireApiAuth helper and protect all 25 unprotected API routes - Add org-tenant scoping to all card, job, stats, and notification queries - Fix SSRF in ai-test, mask secrets in settings API, fix middleware bypass - Fix cards pagination routing, stat filter sync, drag-drop file passing - Add PUT /api/auth/me for profile persistence, stuck job recovery - Fix email watcher MIME type detection Phase 2 - Dynamic Fields & Digital Survey: - Add FormTemplate, FormField, Person, PasswordResetToken models to schema - Add fieldData, formTemplateId, firstName, lastName, personId to ResponseCard - Build FormTemplate CRUD API with field management and org scoping - Build Form Builder UI with field ordering, type config, and section management - Refactor card detail page to render fields dynamically from templates - Add dynamic OCR prompt/schema generation from template fields - Build public survey page at /s/[orgSlug]/[formSlug] with branding - Add QR code generation API and share section component Phase 3 - People & Analytics: - Build People CRUD API with merge and batch auto-link endpoints - Build People list and detail pages with search, merge dialog - Add auto-link logic in OCR completion to match/create Person records - Add /api/stats/trends endpoint with time series and team activity - Build Reports page with Recharts (area charts, bar charts, pipeline) - Upgrade dashboard with sparklines and People stat card Phase 4 - UX Polish: - Replace silent error handling with toast notifications across all pages - Add loading skeletons, differentiated empty states - Add ARIA labels, skip-to-content link, accessible column toggle - Add forgot password flow, Cmd+K command palette, Collection Days pages - Unify Echo branding and theme toggle consistency Made-with: Cursor
2026-04-17 00:29:26 -04:00
/^\/(.*\.(ico|svg|png|jpg|jpeg|gif|webp|woff2?|ttf|css|js|map))$/.test(pathname);
if (isStaticFile) {
return NextResponse.next();
}
if (isPublic(pathname)) {
return NextResponse.next();
}
const secureCookie =
req.headers.get("x-forwarded-proto") === "https" ||
req.nextUrl.protocol === "https:";
const token = await getToken({
req,
secret: process.env.AUTH_SECRET,
secureCookie,
});
if (!token) {
if (pathname === "/login" || pathname === "/signup") {
return NextResponse.next();
}
const welcomeUrl = new URL("/welcome", req.url);
return NextResponse.redirect(welcomeUrl);
}
const hasOrg = !!token.orgId;
const onboardingDone = token.onboardingComplete === true;
function isWorkspaceSetupExempt(p: string) {
return workspaceSetupExemptPaths.some(
(x) => p === x || p.startsWith(x + "/")
);
}
if (!hasOrg && !isWorkspaceSetupExempt(pathname)) {
return NextResponse.redirect(new URL("/workspace-setup", req.url));
}
if (hasOrg && !onboardingDone && !isOnboardingExempt(pathname)) {
return NextResponse.redirect(new URL("/onboarding", req.url));
}
return NextResponse.next();
}
export const config = {
matcher: ["/((?!_next/static|_next/image|favicon.ico).*)"],
};