11import { NextResponse } from 'next/server' ;
22import { createClient } from '@supabase/supabase-js' ;
3+ import { createServerClient } from '@supabase/ssr' ;
4+ import { cookies } from 'next/headers' ;
35
4- function getSupabaseClient ( ) {
5- return createClient (
6- process . env . NEXT_PUBLIC_SUPABASE_URL ! ,
7- process . env . SUPABASE_SERVICE_ROLE_KEY !
8- ) ;
6+ // Server-side clients
7+ function getServiceClient ( ) {
8+ return createClient (
9+ process . env . NEXT_PUBLIC_SUPABASE_URL ! ,
10+ process . env . SUPABASE_SERVICE_ROLE_KEY !
11+ ) ;
12+ }
13+
14+ async function getServerClient ( ) {
15+ const cookieStore = await cookies ( ) ;
16+ return createServerClient (
17+ process . env . NEXT_PUBLIC_SUPABASE_URL ! ,
18+ process . env . NEXT_PUBLIC_SUPABASE_ANON_KEY ! ,
19+ {
20+ cookies : {
21+ getAll ( ) {
22+ return cookieStore . getAll ( ) ;
23+ } ,
24+ setAll ( cookiesToSet ) {
25+ cookiesToSet . forEach ( ( { name, value, options } ) => {
26+ cookieStore . set ( name , value , options ) ;
27+ } ) ;
28+ } ,
29+ } ,
30+ }
31+ ) ;
32+ }
33+
34+ async function requireAdmin ( ) {
35+ const supa = await getServerClient ( ) ;
36+ const { data : { user } , error } = await supa . auth . getUser ( ) ;
37+ if ( error || ! user ) {
38+ return { ok : false , resp : NextResponse . json ( { error : 'Unauthorized' } , { status : 401 } ) } ;
39+ }
40+ // Check admin flag from profiles (service client to bypass RLS for lookup only)
41+ const svc = getServiceClient ( ) ;
42+ const { data : profile , error : pErr } = await svc . from ( 'profiles' ) . select ( 'is_admin' ) . eq ( 'id' , user . id ) . single ( ) ;
43+ if ( pErr || ! profile ?. is_admin ) {
44+ return { ok : false , resp : NextResponse . json ( { error : 'Forbidden' } , { status : 403 } ) } ;
45+ }
46+ return { ok : true } ;
947}
1048
1149export async function GET ( ) {
1250 try {
13- const supabase = getSupabaseClient ( ) ;
51+ const auth = await requireAdmin ( ) ;
52+ if ( ! auth . ok ) return auth . resp ;
53+
54+ const supabase = getServiceClient ( ) ;
1455
1556 const { data, error } = await supabase
1657 . from ( 'core_team_applications' )
17- . select ( '* ' )
58+ . select ( 'id,first_name,last_name,email,phone,location,occupation,company,experience,skills,portfolio,preferred_role,availability,commitment,motivation,vision,previous_experience,social_media,references_info,additional_info,status,user_id,created_at,updated_at ' )
1859 . order ( 'created_at' , { ascending : false } ) ;
1960
2061 if ( error ) {
@@ -31,14 +72,18 @@ export async function GET() {
3172
3273export async function POST ( req : Request ) {
3374 try {
75+ const auth = await requireAdmin ( ) ;
76+ if ( ! auth . ok ) return auth . resp ;
77+
3478 const body = await req . json ( ) ;
35- const { id, status, notes } = body ;
79+ const { id, status, notes } = body as { id ?: number ; status ?: string ; notes ?: string } ;
3680
37- if ( ! id || ! status ) {
38- return NextResponse . json ( { error : 'Missing required fields' } , { status : 400 } ) ;
81+ const ALLOWED_STATUSES = new Set ( [ 'pending' , 'approved' , 'rejected' ] ) ;
82+ if ( ! id || ! status || ! ALLOWED_STATUSES . has ( status ) ) {
83+ return NextResponse . json ( { error : 'Missing required fields or invalid status' } , { status : 400 } ) ;
3984 }
4085
41- const supabase = getSupabaseClient ( ) ;
86+ const supabase = getServiceClient ( ) ;
4287
4388 const { data, error } = await supabase
4489 . from ( 'core_team_applications' )
@@ -65,14 +110,17 @@ export async function POST(req: Request) {
65110
66111export async function PATCH ( req : Request ) {
67112 try {
113+ const auth = await requireAdmin ( ) ;
114+ if ( ! auth . ok ) return auth . resp ;
115+
68116 const body = await req . json ( ) ;
69117 const { id, ...updates } = body ;
70118
71119 if ( ! id ) {
72120 return NextResponse . json ( { error : 'Missing application ID' } , { status : 400 } ) ;
73121 }
74122
75- const supabase = getSupabaseClient ( ) ;
123+ const supabase = getServiceClient ( ) ;
76124
77125 const { data, error } = await supabase
78126 . from ( 'core_team_applications' )
0 commit comments