feat(auth): phase 3 — replace Supabase GoTrue with bcryptjs+jose

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>
This commit is contained in:
2026-07-23 16:20:04 +08:00
co-authored by Claude Sonnet 4.6
parent a95273b182
commit d18d29168a
67 changed files with 966 additions and 591 deletions
+12 -15
View File
@@ -3,6 +3,7 @@ export const dynamic = 'force-dynamic'
import Link from 'next/link'
import { createClient } from '@/lib/supabase/server'
import { redirect } from 'next/navigation'
import { getSession } from '@/lib/auth/get-session'
import { StatCard } from '@/components/dashboard/stat-card'
const STATUS_LABELS: Record<string, string> = {
@@ -24,17 +25,13 @@ const STATUS_COLORS: Record<string, string> = {
}
export default async function SupervisorPage() {
const supabase = await createClient()
const { data: { user } } = await supabase.auth.getUser()
if (!user) redirect('/login')
const session = await getSession()
if (!session) redirect('/login')
if (!['supervisor', 'admin'].includes(session.role)) redirect('/')
const { data: profile } = await supabase
.from('users')
.select('name, site_id, role')
.eq('id', user.id)
.single()
if (!profile || !['supervisor', 'admin'].includes(profile.role)) redirect('/')
if (!profile.site_id) {
const supabase = await createClient()
if (!session.siteId) {
return (
<main className="max-w-4xl mx-auto px-4 py-6">
<h1 className="text-2xl font-bold text-gray-900 mb-4">Supervisor Portal</h1>
@@ -43,14 +40,14 @@ export default async function SupervisorPage() {
)
}
const { data: siteRow } = await supabase.from('sites').select('name').eq('id', profile.site_id).single()
const { data: siteRow } = await supabase.from('sites').select('name').eq('id', session.siteId).single()
const siteName = (siteRow as unknown as { name: string } | null)?.name ?? 'Your Site'
// Resolve incident IDs once to avoid duplicate queries inside Promise.all
const { data: siteIncidents } = await supabase
.from('incidents')
.select('id')
.eq('site_id', profile.site_id)
.eq('site_id', session.siteId)
const incidentIds = siteIncidents?.map(r => r.id) ?? []
const [
@@ -63,19 +60,19 @@ export default async function SupervisorPage() {
supabase
.from('incidents')
.select('id, reference_no, incident_type, status, reported_at')
.eq('site_id', profile.site_id)
.eq('site_id', session.siteId)
.neq('status', 'closed')
.order('reported_at', { ascending: false })
.limit(10),
supabase
.from('incidents')
.select('*', { count: 'exact', head: true })
.eq('site_id', profile.site_id)
.eq('site_id', session.siteId)
.eq('status', 'closed'),
supabase
.from('incidents')
.select('*', { count: 'exact', head: true })
.eq('site_id', profile.site_id)
.eq('site_id', session.siteId)
.neq('status', 'closed'),
supabase
.from('capa_actions')