Files
beenvoice/src/middleware.ts
Sean O'Connor a1b40e7a9c Add Turso/Vercel deployment configuration
- Updated database connection to support Turso auth token
- Added vercel.json with bun build configuration
- Updated environment schema for production deployment
- Added new features and components for production readiness
2025-07-12 01:42:43 -04:00

55 lines
1.8 KiB
TypeScript

import { NextResponse } from "next/server";
import type { NextRequest } from "next/server";
export function middleware(request: NextRequest) {
const { pathname } = request.nextUrl;
// Define public routes that don't require authentication
const publicRoutes = ["/", "/auth/signin", "/auth/register"];
// Define API routes that should be handled separately
const apiRoutes = ["/api/auth", "/api/trpc"];
// Allow API routes to pass through
if (apiRoutes.some((route) => pathname.startsWith(route))) {
return NextResponse.next();
}
// Allow public routes for everyone
if (publicRoutes.includes(pathname)) {
return NextResponse.next();
}
// Check for session token in cookies (Auth.js v5 cookie names)
const sessionToken =
request.cookies.get("authjs.session-token")?.value ||
request.cookies.get("__Secure-authjs.session-token")?.value ||
request.cookies.get("next-auth.session-token")?.value ||
request.cookies.get("__Secure-next-auth.session-token")?.value;
// If no session token, redirect to sign-in
if (!sessionToken) {
const signInUrl = new URL("/auth/signin", request.url);
signInUrl.searchParams.set("callbackUrl", request.url);
return NextResponse.redirect(signInUrl);
}
// Session token exists, allow the request to proceed
// The actual pages will validate the token properly
return NextResponse.next();
}
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api/auth (Auth.js API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* - public folder files
*/
"/((?!api/auth|_next/static|_next/image|favicon.ico|.*\\.png$|.*\\.jpg$|.*\\.jpeg$|.*\\.gif$|.*\\.svg$).*)",
],
};