Files
Webklar.com/middleware.ts
Basilosaurusrex f027651f9b main repo
2025-11-24 18:09:40 +01:00

28 lines
843 B
TypeScript

import { createMiddlewareClient } from '@supabase/auth-helpers-nextjs'
import { NextResponse } from 'next/server'
import type { NextRequest } from 'next/server'
export async function middleware(req: NextRequest) {
const res = NextResponse.next()
const supabase = createMiddlewareClient({ req, res })
const {
data: { session },
} = await supabase.auth.getSession()
// If no session and trying to access protected route
if (!session && req.nextUrl.pathname.startsWith('/protected')) {
return NextResponse.redirect(new URL('/auth', req.url))
}
// If session exists and trying to access auth page
if (session && req.nextUrl.pathname.startsWith('/auth')) {
return NextResponse.redirect(new URL('/protected', req.url))
}
return res
}
export const config = {
matcher: ['/protected/:path*', '/auth/:path*']
}