Files
ProjectE/apps/web/middleware.ts
T
mbatchelder 73335484f8 feat: migrate from PocketBase to PostgreSQL with Drizzle ORM
- Add @project-e/db package with Drizzle schema and migrations
- Replace PocketBase client with PostgreSQL-based database client
- Migrate auth from custom to NextAuth.js
- Add Docker Compose with PostgreSQL container
- Update worker to use new database client
- Remove PocketBase-specific files and migrations
- Add drizzle config and initial migration
2026-07-24 07:08:29 -04:00

50 lines
1.6 KiB
TypeScript

import { NextRequest, NextResponse } from 'next/server';
export function middleware(request: NextRequest) {
// Check if user is authenticated
const token = request.cookies.get('next-auth.session-token')?.value
|| request.cookies.get('__Secure-next-auth.session-token')?.value;
// If no token and trying to access protected routes, redirect to login
const protectedRoutes = [
'/dashboard', '/tasks', '/habits', '/projects', '/notes', '/reports',
'/calendar', '/analytics', '/agents', '/settings',
];
if (!token && protectedRoutes.some((route) => request.nextUrl.pathname === route || request.nextUrl.pathname.startsWith(`${route}/`))) {
const loginUrl = new URL('/login', request.url);
return NextResponse.redirect(loginUrl);
}
// Create response
const response = NextResponse.next();
// Forward proxy headers for proper client IP detection
// Nginx Proxy Manager sets X-Forwarded-For and X-Forwarded-Proto
const forwardedFor = request.headers.get('x-forwarded-for');
const forwardedProto = request.headers.get('x-forwarded-proto');
if (forwardedFor) {
response.headers.set('x-real-ip', forwardedFor.split(',')[0].trim());
}
if (forwardedProto) {
response.headers.set('x-forwarded-proto', forwardedProto);
}
return response;
}
export const config = {
matcher: [
/*
* Match all request paths except:
* - api/auth routes (login, logout, etc.)
* - _next/static (static files)
* - _next/image (image optimization)
* - favicon.ico (favicon)
* - public files (public folder)
*/
'/((?!api/auth|_next/static|_next/image|favicon.ico|public).*)',
],
};