25 lines
700 B
TypeScript
25 lines
700 B
TypeScript
import { NextResponse } from 'next/server';
|
|
import type { NextRequest } from 'next/server';
|
|
|
|
const PUBLIC_PATHS = ['/login', '/auth/callback'];
|
|
|
|
export function middleware(request: NextRequest) {
|
|
const { pathname } = request.nextUrl;
|
|
const isPublic = PUBLIC_PATHS.some((p) => pathname.startsWith(p));
|
|
const hasSession = request.cookies.has('sf_session');
|
|
|
|
if (!isPublic && !hasSession) {
|
|
return NextResponse.redirect(new URL('/login', request.url));
|
|
}
|
|
|
|
if (pathname === '/login' && hasSession) {
|
|
return NextResponse.redirect(new URL('/', request.url));
|
|
}
|
|
|
|
return NextResponse.next();
|
|
}
|
|
|
|
export const config = {
|
|
matcher: ['/((?!_next/static|_next/image|favicon.ico).*)'],
|
|
};
|