27 lines
784 B
TypeScript
27 lines
784 B
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { getSession } from './lib/session';
|
|
|
|
const protectedRoutes = ['/'];
|
|
const publicRoutes = ['/login'];
|
|
|
|
export async function middleware(req: NextRequest) {
|
|
const path = req.nextUrl.pathname;
|
|
const isProtectedRoute = protectedRoutes.includes(path);
|
|
const isPublicRoute = publicRoutes.includes(path);
|
|
|
|
const session = await getSession();
|
|
|
|
if (isProtectedRoute && !session) {
|
|
return NextResponse.redirect(new URL('/login', req.nextUrl));
|
|
}
|
|
|
|
if (isPublicRoute && session && !req.nextUrl.searchParams.has('from')) {
|
|
return NextResponse.redirect(new URL('/', req.nextUrl));
|
|
}
|
|
|
|
return NextResponse.next();
|
|
}
|
|
|
|
export const config = {
|
|
matcher: ['/((?!api|_next/static|_next/image|.*\\.png$).*)'],
|
|
};
|