Files
neta/lib/supabase/middleware.ts
T
Poyraz 72a0b38f3e Remove obsolete scripts and configuration files for self-hosting and database migrations
- Deleted `pnpm-workspace.yaml` as it is no longer needed.
- Removed `apply-migrations.sh` script that handled database migrations.
- Eliminated `generate-full-stack-env.mjs` script for environment variable generation.
- Deleted `selfhost-backup.sh` script for creating backups of the self-hosted environment.
- Removed `selfhost-doctor.sh` script for health checks of the self-hosted services.
- Deleted `selfhost-restore.sh` script for restoring backups in the self-hosted environment.
2026-06-16 09:57:32 +03:00

63 lines
1.7 KiB
TypeScript

import { createServerClient } from '@supabase/ssr'
import { NextResponse, type NextRequest } from 'next/server'
export async function updateSession(request: NextRequest) {
const pathname = request.nextUrl.pathname
if (
pathname.startsWith('/login') ||
pathname.startsWith('/register') ||
pathname.startsWith('/auth') ||
pathname.startsWith('/forgot-password')
) {
return NextResponse.next({ request })
}
let supabaseResponse = NextResponse.next({
request,
})
const supabase = createServerClient(
process.env.NEXT_PUBLIC_SUPABASE_URL!,
process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
{
cookies: {
getAll() {
return request.cookies.getAll()
},
setAll(cookiesToSet) {
cookiesToSet.forEach(({ name, value }) => request.cookies.set(name, value))
supabaseResponse = NextResponse.next({
request,
})
cookiesToSet.forEach(({ name, value, options }) =>
supabaseResponse.cookies.set(name, value, options)
)
},
},
}
)
// IMPORTANT: Avoid writing any logic between createServerClient and
// supabase.auth.getUser(). A simple mistake could make it very hard to debug
// issues with users being randomly logged out.
const {
data: { user },
} = await supabase.auth.getUser()
if (
!user &&
!pathname.startsWith('/login') &&
!pathname.startsWith('/register') &&
!pathname.startsWith('/auth')
) {
// no user, potentially respond by redirecting the user to the login page
const url = request.nextUrl.clone()
url.pathname = '/login'
return NextResponse.redirect(url)
}
return supabaseResponse
}