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:
@@ -3,18 +3,17 @@ export const dynamic = 'force-dynamic'
|
||||
import Link from 'next/link'
|
||||
import { redirect } from 'next/navigation'
|
||||
import { createClient } from '@/lib/supabase/server'
|
||||
import { getSession } from '@/lib/auth/get-session'
|
||||
import { UserManager, type AdminUser, type SiteOption } from '@/components/admin/user-manager'
|
||||
import { SiteZoneManager, type SiteWithZones } from '@/components/admin/site-zone-manager'
|
||||
import { TruckManager, type Truck } from '@/components/admin/truck-manager'
|
||||
|
||||
export default async function AdminHome() {
|
||||
const supabase = await createClient()
|
||||
const { data: { user } } = await supabase.auth.getUser()
|
||||
if (!user) redirect('/login')
|
||||
const session = await getSession()
|
||||
if (!session) redirect('/login')
|
||||
if (session.role !== 'admin') redirect('/login')
|
||||
|
||||
const { data: profile } = await supabase
|
||||
.from('users').select('role').eq('id', user.id).single()
|
||||
if (!profile || profile.role !== 'admin') redirect('/login')
|
||||
const supabase = await createClient()
|
||||
|
||||
const [{ data: users }, { data: sites }, { data: trucks }] = await Promise.all([
|
||||
supabase
|
||||
|
||||
@@ -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'
|
||||
import { CapaOwnerActions } from '@/components/capa/capa-owner-actions'
|
||||
import { CapaOwnerNotesForm } from '@/components/capa/capa-owner-notes-form'
|
||||
@@ -28,17 +29,16 @@ const STATUS_COLORS: Record<string, string> = {
|
||||
}
|
||||
|
||||
export default async function CapaOwnerPage() {
|
||||
const supabase = await createClient()
|
||||
const { data: { user } } = await supabase.auth.getUser()
|
||||
if (!user) redirect('/login')
|
||||
const session = await getSession()
|
||||
if (!session) redirect('/login')
|
||||
if (!['capa_owner', 'admin', 'hse', 'supervisor'].includes(session.role)) redirect('/')
|
||||
|
||||
const { data: profile } = await supabase.from('users').select('name, role').eq('id', user.id).single()
|
||||
if (!profile || !['capa_owner', 'admin', 'hse', 'supervisor'].includes(profile.role)) redirect('/')
|
||||
const supabase = await createClient()
|
||||
|
||||
const { data: capas } = await supabase
|
||||
.from('capa_actions')
|
||||
.select('id, description, due_date, priority, status, incident_id, owner_notes, incidents (reference_no)')
|
||||
.eq('owner_user_id', user.id)
|
||||
.eq('owner_user_id', session.sub)
|
||||
.order('due_date', { ascending: true, nullsFirst: false })
|
||||
|
||||
const rows = capas ?? []
|
||||
|
||||
@@ -3,6 +3,7 @@ export const dynamic = 'force-dynamic'
|
||||
import { notFound, redirect } from 'next/navigation'
|
||||
import Link from 'next/link'
|
||||
import { createClient } from '@/lib/supabase/server'
|
||||
import { getSession } from '@/lib/auth/get-session'
|
||||
import { VerifyForm } from '@/components/capa/verify-form'
|
||||
import { CloseCapaButton } from '@/components/capa/close-capa-button'
|
||||
|
||||
@@ -18,12 +19,10 @@ const PRIORITY_BADGE: Record<string, string> = {
|
||||
|
||||
export default async function CapaDetailPage({ params }: Props) {
|
||||
const { id } = await params
|
||||
const supabase = await createClient()
|
||||
const { data, error: authError } = await supabase.auth.getUser()
|
||||
if (authError || !data?.user) redirect(`/login?redirect=/hse/capa/${id}`)
|
||||
const session = await getSession()
|
||||
if (!session) redirect(`/login?redirect=/hse/capa/${id}`)
|
||||
|
||||
const { data: profile } = await supabase
|
||||
.from('users').select('role').eq('id', data.user.id).single()
|
||||
const supabase = await createClient()
|
||||
|
||||
const { data: capa } = await supabase
|
||||
.from('capa_actions')
|
||||
@@ -39,7 +38,7 @@ export default async function CapaDetailPage({ params }: Props) {
|
||||
|
||||
if (!capa) notFound()
|
||||
|
||||
const isHse = profile && ['hse', 'admin'].includes(profile.role)
|
||||
const isHse = session && ['hse', 'admin'].includes(session.role)
|
||||
const status = (capa as { status: string }).status
|
||||
|
||||
return (
|
||||
|
||||
@@ -3,12 +3,14 @@ export const dynamic = 'force-dynamic'
|
||||
import { redirect } from 'next/navigation'
|
||||
import Link from 'next/link'
|
||||
import { createClient } from '@/lib/supabase/server'
|
||||
import { getSession } from '@/lib/auth/get-session'
|
||||
import { CapaBoard } from '@/components/capa/capa-board'
|
||||
|
||||
export default async function CapaListPage() {
|
||||
const session = await getSession()
|
||||
if (!session) redirect('/login?redirect=/hse/capa')
|
||||
|
||||
const supabase = await createClient()
|
||||
const { data, error: authError } = await supabase.auth.getUser()
|
||||
if (authError || !data?.user) redirect('/login?redirect=/hse/capa')
|
||||
|
||||
const { data: capas } = await supabase
|
||||
.from('capa_actions')
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
import { notFound, redirect } from 'next/navigation'
|
||||
import Link from 'next/link'
|
||||
import { createClient } from '@/lib/supabase/server'
|
||||
import { getSession } from '@/lib/auth/get-session'
|
||||
import { CapaForm } from '@/components/capa/capa-form'
|
||||
|
||||
interface Props {
|
||||
@@ -9,13 +10,11 @@ interface Props {
|
||||
|
||||
export default async function NewCapaPage({ params }: Props) {
|
||||
const { id } = await params
|
||||
const supabase = await createClient()
|
||||
const { data, error: authError } = await supabase.auth.getUser()
|
||||
if (authError || !data?.user) redirect(`/login?redirect=/hse/incidents/${id}/capa/new`)
|
||||
const session = await getSession()
|
||||
if (!session) redirect(`/login?redirect=/hse/incidents/${id}/capa/new`)
|
||||
if (!['hse', 'admin'].includes(session.role)) redirect('/hse/incidents')
|
||||
|
||||
const { data: profile } = await supabase
|
||||
.from('users').select('role').eq('id', data.user.id).single()
|
||||
if (!profile || !['hse', 'admin'].includes(profile.role)) redirect('/hse/incidents')
|
||||
const supabase = await createClient()
|
||||
|
||||
const { data: incident } = await supabase
|
||||
.from('incidents').select('id, reference_no, status').eq('id', id).single()
|
||||
|
||||
@@ -3,6 +3,7 @@ export const dynamic = 'force-dynamic'
|
||||
import { notFound, redirect } from 'next/navigation'
|
||||
import Link from 'next/link'
|
||||
import { createClient } from '@/lib/supabase/server'
|
||||
import { getSession } from '@/lib/auth/get-session'
|
||||
import { InvestigationForm } from '@/components/incidents/investigation-form'
|
||||
|
||||
interface Props {
|
||||
@@ -11,15 +12,12 @@ interface Props {
|
||||
|
||||
export default async function InvestigationPage({ params }: Props) {
|
||||
const { id } = await params
|
||||
const session = await getSession()
|
||||
if (!session) redirect(`/login?redirect=/hse/incidents/${id}/investigation`)
|
||||
if (!['hse', 'admin'].includes(session.role)) redirect('/hse/incidents')
|
||||
|
||||
const supabase = await createClient()
|
||||
|
||||
const { data, error: authError } = await supabase.auth.getUser()
|
||||
if (authError || !data?.user) redirect(`/login?redirect=/hse/incidents/${id}/investigation`)
|
||||
|
||||
const { data: profile } = await supabase
|
||||
.from('users').select('role').eq('id', data.user.id).single()
|
||||
if (!profile || !['hse', 'admin'].includes(profile.role)) redirect('/hse/incidents')
|
||||
|
||||
const { data: incident } = await supabase
|
||||
.from('incidents')
|
||||
.select('id, reference_no, status')
|
||||
|
||||
@@ -3,6 +3,7 @@ export const dynamic = 'force-dynamic'
|
||||
import { notFound, redirect } from 'next/navigation'
|
||||
import Link from 'next/link'
|
||||
import { createClient } from '@/lib/supabase/server'
|
||||
import { getSession } from '@/lib/auth/get-session'
|
||||
import { TriageForm } from '@/components/incidents/triage-form'
|
||||
|
||||
interface Props {
|
||||
@@ -11,15 +12,12 @@ interface Props {
|
||||
|
||||
export default async function TriagePage({ params }: Props) {
|
||||
const { id } = await params
|
||||
const session = await getSession()
|
||||
if (!session) redirect(`/login?redirect=/hse/incidents/${id}/triage`)
|
||||
if (!['hse', 'admin'].includes(session.role)) redirect('/hse/incidents')
|
||||
|
||||
const supabase = await createClient()
|
||||
|
||||
const { data, error: authError } = await supabase.auth.getUser()
|
||||
if (authError || !data?.user) redirect(`/login?redirect=/hse/incidents/${id}/triage`)
|
||||
|
||||
const { data: profile } = await supabase
|
||||
.from('users').select('role').eq('id', data.user.id).single()
|
||||
if (!profile || !['hse', 'admin'].includes(profile.role)) redirect('/hse/incidents')
|
||||
|
||||
const { data: incident } = await supabase
|
||||
.from('incidents')
|
||||
.select('id, reference_no, incident_type, status, severity')
|
||||
|
||||
@@ -1,20 +1,17 @@
|
||||
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'
|
||||
|
||||
export default async function HsePage() {
|
||||
const supabase = await createClient()
|
||||
const { data: { user } } = await supabase.auth.getUser()
|
||||
if (!user) redirect('/login')
|
||||
|
||||
const { data: profile } = await supabase.from('users').select('name').eq('id', user.id).single()
|
||||
const session = await getSession()
|
||||
if (!session) redirect('/login')
|
||||
|
||||
return (
|
||||
<main className="max-w-4xl mx-auto px-4 py-6">
|
||||
<h1 className="text-2xl font-bold text-gray-900 mb-1">HSE Officer Portal</h1>
|
||||
<p className="text-gray-500 text-sm mb-8">Welcome, {profile?.name ?? user.email}</p>
|
||||
<p className="text-gray-500 text-sm mb-8">Welcome, {session.name}</p>
|
||||
<div className="grid grid-cols-2 gap-4 sm:grid-cols-3">
|
||||
<Link href="/hse/incidents" className="bg-white rounded-xl shadow-sm p-5 hover:shadow-md transition-shadow border border-gray-100">
|
||||
<div className="text-2xl mb-2">📋</div>
|
||||
|
||||
@@ -2,15 +2,15 @@ export const dynamic = 'force-dynamic'
|
||||
|
||||
import { redirect } from 'next/navigation'
|
||||
import { createClient } from '@/lib/supabase/server'
|
||||
import { getSession } from '@/lib/auth/get-session'
|
||||
import { ApiKeyForm } from '@/components/settings/api-key-form'
|
||||
|
||||
export default async function SettingsPage() {
|
||||
const supabase = await createClient()
|
||||
const { data: { user } } = await supabase.auth.getUser()
|
||||
if (!user) redirect('/login')
|
||||
const session = await getSession()
|
||||
if (!session) redirect('/login')
|
||||
if (session.role !== 'admin') redirect('/hse/dashboard')
|
||||
|
||||
const { data: profile } = await supabase.from('users').select('role').eq('id', user.id).single()
|
||||
if (!profile || profile.role !== 'admin') redirect('/hse/dashboard')
|
||||
const supabase = await createClient()
|
||||
|
||||
const { data: settings } = await supabase
|
||||
.from('app_settings')
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
export const dynamic = 'force-dynamic'
|
||||
|
||||
import { createClient } from '@/lib/supabase/server'
|
||||
import { redirect } from 'next/navigation'
|
||||
import { getSession } from '@/lib/auth/get-session'
|
||||
import { Sidebar } from '@/components/layout/sidebar'
|
||||
|
||||
export default async function ProtectedLayout({
|
||||
@@ -9,25 +9,15 @@ export default async function ProtectedLayout({
|
||||
}: {
|
||||
children: React.ReactNode
|
||||
}) {
|
||||
const supabase = await createClient()
|
||||
const {
|
||||
data: { user },
|
||||
} = await supabase.auth.getUser()
|
||||
|
||||
if (!user) redirect('/login')
|
||||
|
||||
const { data: profile } = await supabase
|
||||
.from('users')
|
||||
.select('role, name, email')
|
||||
.eq('id', user.id)
|
||||
.single()
|
||||
const session = await getSession()
|
||||
if (!session) redirect('/login')
|
||||
|
||||
return (
|
||||
<div className="min-h-screen bg-gray-50">
|
||||
<Sidebar
|
||||
role={profile?.role ?? 'reporter'}
|
||||
userName={profile?.name ?? user.email ?? ''}
|
||||
userEmail={profile?.email ?? user.email ?? ''}
|
||||
role={session.role}
|
||||
userName={session.name}
|
||||
userEmail=""
|
||||
/>
|
||||
<div className="sm:ml-60 pb-16 sm:pb-0">
|
||||
{children}
|
||||
|
||||
@@ -2,16 +2,16 @@ export const dynamic = 'force-dynamic'
|
||||
|
||||
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'
|
||||
import { RiskFlagsPanel } from '@/components/dashboard/risk-flags-panel'
|
||||
|
||||
export default async function ManagementPage() {
|
||||
const supabase = await createClient()
|
||||
const { data: { user } } = await supabase.auth.getUser()
|
||||
if (!user) redirect('/login')
|
||||
const session = await getSession()
|
||||
if (!session) redirect('/login')
|
||||
if (!['management', 'admin'].includes(session.role)) redirect('/')
|
||||
|
||||
const { data: profile } = await supabase.from('users').select('role').eq('id', user.id).single()
|
||||
if (!profile || !['management', 'admin'].includes(profile.role)) redirect('/')
|
||||
const supabase = await createClient()
|
||||
|
||||
const now = new Date()
|
||||
const thisMonthStart = new Date(now.getFullYear(), now.getMonth(), 1).toISOString()
|
||||
|
||||
@@ -3,6 +3,7 @@ export const dynamic = 'force-dynamic'
|
||||
import Link from 'next/link'
|
||||
import { createClient } from '@/lib/supabase/server'
|
||||
import { redirect, notFound } from 'next/navigation'
|
||||
import { getSession } from '@/lib/auth/get-session'
|
||||
|
||||
const STATUS_LABELS: Record<string, string> = {
|
||||
reported: 'Reported', triaged: 'Triaged', investigating: 'Investigating',
|
||||
@@ -20,9 +21,10 @@ export default async function ReporterIncidentDetail({
|
||||
params: Promise<{ id: string }>
|
||||
}) {
|
||||
const { id } = await params
|
||||
const session = await getSession()
|
||||
if (!session) redirect('/login')
|
||||
|
||||
const supabase = await createClient()
|
||||
const { data: { user } } = await supabase.auth.getUser()
|
||||
if (!user) redirect('/login')
|
||||
|
||||
const { data: inc } = await supabase
|
||||
.from('incidents')
|
||||
@@ -33,7 +35,7 @@ export default async function ReporterIncidentDetail({
|
||||
capa_actions (id, description, status, due_date)
|
||||
`)
|
||||
.eq('id', id)
|
||||
.eq('reported_by', user.id)
|
||||
.eq('reported_by', session.sub)
|
||||
.single()
|
||||
|
||||
if (!inc) notFound()
|
||||
|
||||
@@ -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'
|
||||
|
||||
const STATUS_LABELS: Record<string, string> = {
|
||||
reported: 'Reported',
|
||||
@@ -28,16 +29,15 @@ const TYPE_LABELS: Record<string, string> = {
|
||||
}
|
||||
|
||||
export default async function ReporterPage() {
|
||||
const supabase = await createClient()
|
||||
const { data: { user } } = await supabase.auth.getUser()
|
||||
if (!user) redirect('/login')
|
||||
const session = await getSession()
|
||||
if (!session) redirect('/login')
|
||||
|
||||
const { data: profile } = await supabase.from('users').select('name').eq('id', user.id).single()
|
||||
const supabase = await createClient()
|
||||
|
||||
const { data: incidents } = await supabase
|
||||
.from('incidents')
|
||||
.select('id, reference_no, incident_type, status, reported_at, severity, sites (name)')
|
||||
.eq('reported_by', user.id)
|
||||
.eq('reported_by', session.sub)
|
||||
.order('reported_at', { ascending: false })
|
||||
|
||||
const rows = incidents ?? []
|
||||
@@ -49,7 +49,7 @@ export default async function ReporterPage() {
|
||||
<div className="flex items-center justify-between mb-6">
|
||||
<div>
|
||||
<h1 className="text-2xl font-bold text-gray-900">My Reports</h1>
|
||||
<p className="text-sm text-gray-400 mt-0.5">Welcome, {profile?.name ?? user.email}</p>
|
||||
<p className="text-sm text-gray-400 mt-0.5">Welcome, {session.name}</p>
|
||||
</div>
|
||||
<Link
|
||||
href="/report"
|
||||
|
||||
@@ -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')
|
||||
|
||||
Reference in New Issue
Block a user