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
+9 -14
View File
@@ -2,6 +2,7 @@ export const dynamic = 'force-dynamic'
import { NextRequest, NextResponse } from 'next/server'
import { createClient } from '@/lib/supabase/server'
import { getSession } from '@/lib/auth/get-session'
const ALLOWED_KEYS = [
'DEEPSEEK_API_KEY',
@@ -11,18 +12,11 @@ const ALLOWED_KEYS = [
] as const
type SettingKey = typeof ALLOWED_KEYS[number]
async function requireAdmin(supabase: Awaited<ReturnType<typeof createClient>>) {
const { data: { user }, error } = await supabase.auth.getUser()
if (error || !user) return null
const { data: profile } = await supabase.from('users').select('role').eq('id', user.id).single()
if (!profile || profile.role !== 'admin') return null
return user
}
export async function GET() {
const session = await getSession()
if (!session || session.role !== 'admin') return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
const supabase = await createClient()
const user = await requireAdmin(supabase)
if (!user) return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
const { data } = await supabase.from('app_settings').select('key, value, updated_at')
const masked = (data ?? []).map(row => ({
@@ -35,9 +29,10 @@ export async function GET() {
}
export async function POST(request: NextRequest) {
const session = await getSession()
if (!session || session.role !== 'admin') return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
const supabase = await createClient()
const user = await requireAdmin(supabase)
if (!user) return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
let body: { key?: string; value?: string }
try { body = await request.json() } catch {
@@ -55,13 +50,13 @@ export async function POST(request: NextRequest) {
key: body.key,
value: body.value,
updated_at: new Date().toISOString(),
updated_by: user.id,
updated_by: session.sub,
})
if (error) return NextResponse.json({ error: 'Save failed' }, { status: 500 })
await supabase.rpc('write_audit_log', {
p_table_name: 'app_settings',
p_record_id: user.id,
p_record_id: session.sub,
p_action: 'UPDATE',
p_new_value: { key: body.key, set: Boolean(body.value) } as never,
})