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,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')
|
||||
|
||||
Reference in New Issue
Block a user