Files
Compass/middleware.ts
MartinBraquet e970713dcc Update Auth
2025-07-27 00:08:26 +02:00

48 lines
1.5 KiB
TypeScript

import { NextResponse } from 'next/server';
import type { NextRequest } from 'next/server';
import { getToken } from 'next-auth/jwt';
const publicPaths = ['/', '/login', '/signup', '/api/auth/signin', '/api/auth/signout'];
export async function middleware(req: NextRequest) {
const { pathname } = req.nextUrl;
const token = await getToken({ req });
// Allow access to public paths and static files
if (
publicPaths.some(path => path === pathname) ||
pathname.startsWith('/_next') ||
pathname.startsWith('/favicon.ico') ||
pathname.startsWith('/public/')
) {
// If user is logged in and tries to access auth pages, redirect to dashboard
if (token && (pathname.startsWith('/login') || pathname.startsWith('/signup'))) {
return NextResponse.redirect(new URL('/dashboard', req.url));
}
return NextResponse.next();
}
// If no token and not a public path, redirect to login
if (!token) {
const loginUrl = new URL('/login', req.url);
loginUrl.searchParams.set('callbackUrl', pathname);
return NextResponse.redirect(loginUrl);
}
return NextResponse.next();
}
export const config = {
matcher: [
/*
* Match all request paths except for the ones starting with:
* - api (API routes)
* - _next/static (static files)
* - _next/image (image optimization files)
* - favicon.ico (favicon file)
* - public folder
*/
'/((?!api|_next/static|_next/image|favicon.ico|public).*)',
],
};