fix(admin): RLS 재귀 수정 + OAuth 쿠키 + 코드 정리
- RLS: profiles 재귀 참조 → app_metadata 기반으로 교체 - auth callback: 쿠키를 response에 직접 설정 (세션 유지) - middleware: Supabase 세션 갱신 추가 - layout: requireAdmin() 사용으로 통합 (중복 제거) - admin-guard: app_metadata.role 기반 (JWT, DB 쿼리 불필요) - debug 라우트 제거 - config.toml: localhost:3000/3001 redirect URL 추가
This commit is contained in:
parent
46673ee941
commit
eb4c504fea
7 changed files with 142 additions and 53 deletions
|
|
@ -1,5 +1,5 @@
|
|||
// apps/admin/src/lib/admin-guard.ts
|
||||
// RSC용 admin 가드 — profile.role='admin' 체크
|
||||
// RSC용 admin 가드 — app_metadata.role='admin' 체크
|
||||
|
||||
import { redirect } from 'next/navigation'
|
||||
import { getSupabaseServerClient } from './supabase-server'
|
||||
|
|
@ -18,32 +18,22 @@ export async function requireAdmin(): Promise<AdminUser> {
|
|||
redirect('/login')
|
||||
}
|
||||
|
||||
// role은 DB 타입에 미정의이므로 raw 캐스팅
|
||||
const { data: profile } = await supabase
|
||||
.from('profiles')
|
||||
.select('id, name')
|
||||
.eq('id', user.id)
|
||||
.maybeSingle()
|
||||
|
||||
if (!profile) {
|
||||
redirect('/login')
|
||||
}
|
||||
|
||||
// role 별도 조회 (DB 타입에 role 컬럼 미정의)
|
||||
const { data: roleData } = await supabase
|
||||
.from('profiles')
|
||||
.select('role' as 'id')
|
||||
.eq('id', user.id)
|
||||
.maybeSingle()
|
||||
|
||||
const role = (roleData as unknown as { role: string } | null)?.role
|
||||
// app_metadata.role 체크 (JWT에 포함, RLS 재귀 없음)
|
||||
const role = (user.app_metadata as Record<string, unknown>)?.role as string | undefined
|
||||
if (role !== 'admin') {
|
||||
redirect('/unauthorized')
|
||||
}
|
||||
|
||||
// profile 이름 조회 (자기 자신은 기존 RLS로 접근 가능)
|
||||
const { data: profile } = await supabase
|
||||
.from('profiles')
|
||||
.select('name')
|
||||
.eq('id', user.id)
|
||||
.maybeSingle()
|
||||
|
||||
return {
|
||||
id: user.id,
|
||||
email: user.email ?? null,
|
||||
name: (profile as { name: string | null }).name,
|
||||
name: (profile as { name: string | null } | null)?.name ?? null,
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue