Files
MulliganMates/middleware.ts
T
2026-03-18 13:52:46 +01:00

33 lines
978 B
TypeScript

import { NextResponse, type NextRequest } from 'next/server'
import { betterFetch } from '@better-fetch/fetch'
const PUBLIC_PATHS = ['/login', '/api/auth']
type Session = { user: { id: string; email: string } }
export async function middleware(request: NextRequest) {
const { pathname } = request.nextUrl
const isPublic = PUBLIC_PATHS.some((p) => pathname.startsWith(p))
const { data: session } = await betterFetch<Session>('/api/auth/get-session', {
baseURL: `http://localhost:${process.env.PORT ?? 3000}`,
headers: { cookie: request.headers.get('cookie') ?? '' },
})
if (!session && !isPublic) {
return NextResponse.redirect(new URL('/login', request.url))
}
if (session && pathname === '/login') {
return NextResponse.redirect(new URL('/dashboard', request.url))
}
return NextResponse.next()
}
export const config = {
matcher: [
'/((?!_next/static|_next/image|favicon.ico|icons|.*\\.(?:svg|png|jpg|jpeg|gif|webp)$).*)',
],
}