31 lines
775 B
TypeScript
31 lines
775 B
TypeScript
|
|
import { auth } from '@/app/api/auth/[...nextauth]/route';
|
|
import { NextResponse } from 'next/server';
|
|
import type { NextRequest } from 'next/server';
|
|
|
|
export async function middleware(request: NextRequest) {
|
|
const session = await auth();
|
|
const { pathname } = request.nextUrl;
|
|
|
|
const isAuthPage = pathname === '/login';
|
|
|
|
if (isAuthPage) {
|
|
if (session) {
|
|
return NextResponse.redirect(new URL('/admin', request.url));
|
|
}
|
|
return null;
|
|
}
|
|
|
|
if (!session && pathname.startsWith('/admin')) {
|
|
const signInUrl = new URL('/login', request.url);
|
|
signInUrl.searchParams.set('callbackUrl', pathname);
|
|
return NextResponse.redirect(signInUrl);
|
|
}
|
|
|
|
return NextResponse.next();
|
|
}
|
|
|
|
export const config = {
|
|
matcher: ['/admin/:path*', '/login'],
|
|
};
|