Custom auth stack: bcryptjs password hashing (cost 10, GoTrue-compatible), jose JWT session cookies (edge-safe, 8hr TTL), new API routes for login/logout/reset/change-password, middleware rewritten to JWT-only verification with no DB access. All 38 protected pages and API routes migrated from supabase.auth.getUser() to getSession(). Supabase .from() queries retained for Phase 4. lib/db/index.ts refactored to lazy Proxy singleton to avoid module-level throw during Next.js build. tsc: clean, build: clean, tests: 4/4 passed Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
42 lines
1.4 KiB
TypeScript
42 lines
1.4 KiB
TypeScript
export const dynamic = 'force-dynamic'
|
|
|
|
import { NextResponse } from 'next/server'
|
|
import { createClient } from '@/lib/supabase/server'
|
|
import { getSession } from '@/lib/auth/get-session'
|
|
|
|
export async function GET() {
|
|
const session = await getSession()
|
|
if (!session) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
if (!['hse', 'admin', 'management'].includes(session.role))
|
|
return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
|
|
|
|
const supabase = await createClient()
|
|
|
|
const { data: incidents, error } = await supabase
|
|
.from('incidents')
|
|
.select('id, status, incident_type, sites (name)')
|
|
.limit(10000)
|
|
|
|
if (error) return NextResponse.json({ error: 'Failed to fetch stats' }, { status: 500 })
|
|
|
|
const rows = incidents ?? []
|
|
const total = rows.length
|
|
const closed = rows.filter(r => r.status === 'closed').length
|
|
const open = total - closed
|
|
|
|
const by_type: Record<string, number> = {}
|
|
for (const r of rows) {
|
|
by_type[r.incident_type] = (by_type[r.incident_type] ?? 0) + 1
|
|
}
|
|
|
|
const siteMap: Record<string, number> = {}
|
|
for (const r of rows) {
|
|
const name = (r.sites as unknown as { name: string } | null)?.name ?? 'Unknown'
|
|
siteMap[name] = (siteMap[name] ?? 0) + 1
|
|
}
|
|
const by_site = Object.entries(siteMap).map(([name, count]) => ({ name, count }))
|
|
.sort((a, b) => b.count - a.count)
|
|
|
|
return NextResponse.json({ total, open, closed, by_type, by_site })
|
|
}
|