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:
@@ -0,0 +1,10 @@
|
||||
import 'server-only'
|
||||
import { cookies } from 'next/headers'
|
||||
import { verifySession, type SessionPayload } from './session'
|
||||
|
||||
export async function getSession(): Promise<SessionPayload | null> {
|
||||
const cookieStore = await cookies()
|
||||
const token = cookieStore.get('ims_session')?.value
|
||||
if (!token) return null
|
||||
return verifySession(token)
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import bcryptjs from 'bcryptjs'
|
||||
|
||||
const BCRYPT_ROUNDS = 10 // matches Supabase GoTrue default
|
||||
|
||||
export async function hashPassword(plaintext: string): Promise<string> {
|
||||
return bcryptjs.hash(plaintext, BCRYPT_ROUNDS)
|
||||
}
|
||||
|
||||
export async function verifyPassword(plaintext: string, hash: string): Promise<boolean> {
|
||||
return bcryptjs.compare(plaintext, hash)
|
||||
}
|
||||
@@ -1,18 +1,12 @@
|
||||
import { createClient } from '@/lib/supabase/server'
|
||||
import type { SupabaseClient } from '@supabase/supabase-js'
|
||||
import type { User } from '@supabase/supabase-js'
|
||||
import { getSession } from '@/lib/auth/get-session'
|
||||
import type { SessionPayload } from '@/lib/auth/session'
|
||||
|
||||
// Shared guard for /api/admin/* routes: resolves the session and requires
|
||||
// the admin role. Returns user: null when the caller must respond 403.
|
||||
// the admin role. Returns session: null when the caller must respond 403.
|
||||
export async function requireAdmin(): Promise<{
|
||||
supabase: SupabaseClient
|
||||
user: User | null
|
||||
session: SessionPayload | null
|
||||
}> {
|
||||
const supabase = await createClient()
|
||||
const { data: { user }, error } = await supabase.auth.getUser()
|
||||
if (error || !user) return { supabase, user: null }
|
||||
const { data: profile } = await supabase
|
||||
.from('users').select('role').eq('id', user.id).single()
|
||||
if (!profile || profile.role !== 'admin') return { supabase, user: null }
|
||||
return { supabase, user }
|
||||
const session = await getSession()
|
||||
if (!session || session.role !== 'admin') return { session: null }
|
||||
return { session }
|
||||
}
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
import { SignJWT, jwtVerify, type JWTPayload } from 'jose'
|
||||
|
||||
const SESSION_COOKIE = 'ims_session'
|
||||
const SESSION_TTL_SECONDS = 8 * 60 * 60 // 8 hours
|
||||
|
||||
export { SESSION_COOKIE }
|
||||
|
||||
function getSecret(): Uint8Array {
|
||||
const secret = process.env.JWT_SECRET
|
||||
if (!secret) throw new Error('JWT_SECRET not configured')
|
||||
return new TextEncoder().encode(secret)
|
||||
}
|
||||
|
||||
export interface SessionPayload {
|
||||
sub: string // user id (UUID)
|
||||
role: string // user_role enum value
|
||||
siteId: string | null
|
||||
name: string
|
||||
}
|
||||
|
||||
export async function createSession(payload: SessionPayload): Promise<string> {
|
||||
return new SignJWT({ ...payload } as JWTPayload)
|
||||
.setProtectedHeader({ alg: 'HS256' })
|
||||
.setIssuedAt()
|
||||
.setExpirationTime(`${SESSION_TTL_SECONDS}s`)
|
||||
.sign(getSecret())
|
||||
}
|
||||
|
||||
/** Verify and decode a JWT. Returns null on invalid/expired. */
|
||||
export async function verifySession(token: string): Promise<SessionPayload | null> {
|
||||
try {
|
||||
const { payload } = await jwtVerify(token, getSecret())
|
||||
return payload as unknown as SessionPayload
|
||||
} catch {
|
||||
return null
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user