mirror of
https://github.com/soconnor0919/beenvoice.git
synced 2025-12-13 01:24:44 -05:00
- 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
55 lines
1.8 KiB
TypeScript
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$).*)",
|
|
],
|
|
};
|