35 lines
829 B
TypeScript
35 lines
829 B
TypeScript
|
|
import { auth } from '@/auth';
|
||
|
|
import { NextResponse } from 'next/server';
|
||
|
|
import type { NextRequest } from 'next/server';
|
||
|
|
|
||
|
|
const publicRoutes = ['/', '/login'];
|
||
|
|
const authRoutes = ['/api/auth'];
|
||
|
|
|
||
|
|
export async function middleware(request: NextRequest) {
|
||
|
|
const { pathname } = request.nextUrl;
|
||
|
|
|
||
|
|
if (publicRoutes.includes(pathname)) {
|
||
|
|
return NextResponse.next();
|
||
|
|
}
|
||
|
|
|
||
|
|
if (authRoutes.some(route => pathname.startsWith(route))) {
|
||
|
|
return NextResponse.next();
|
||
|
|
}
|
||
|
|
|
||
|
|
const session = await auth();
|
||
|
|
|
||
|
|
if (!session) {
|
||
|
|
const loginUrl = new URL('/login', request.url);
|
||
|
|
loginUrl.searchParams.set('callbackUrl', pathname);
|
||
|
|
return NextResponse.redirect(loginUrl);
|
||
|
|
}
|
||
|
|
|
||
|
|
return NextResponse.next();
|
||
|
|
}
|
||
|
|
|
||
|
|
export const config = {
|
||
|
|
matcher: [
|
||
|
|
'/((?!_next/static|_next/image|favicon.ico|.*\\..*|api/auth).*)',
|
||
|
|
],
|
||
|
|
};
|