mirror of
https://github.com/pacnpal/thrilltrack-explorer.git
synced 2025-12-23 17:11:12 -05:00
Add proper CORS headers to edge function: - Import and apply corsHeadersWithMethods via corsHeaders config - Include CORS headers in all responses (including unauthorized and success cases) This resolves preflight and subsequent requests blocked by CORS policy.
57 lines
1.6 KiB
TypeScript
57 lines
1.6 KiB
TypeScript
import { createEdgeFunction } from '../_shared/edgeFunctionWrapper.ts';
|
|
import { edgeLogger } from '../_shared/logger.ts';
|
|
import { corsHeadersWithMethods } from '../_shared/cors.ts';
|
|
|
|
export default createEdgeFunction(
|
|
{
|
|
name: 'backfill-park-locations',
|
|
requireAuth: true,
|
|
corsHeaders: corsHeadersWithMethods,
|
|
},
|
|
async (req, context, supabase) => {
|
|
edgeLogger.info('Starting park location backfill', { requestId: context.requestId });
|
|
|
|
// Check if user is superuser
|
|
const { data: profile, error: profileError } = await supabase
|
|
.from('user_profiles')
|
|
.select('role')
|
|
.eq('id', context.user.id)
|
|
.single();
|
|
|
|
if (profileError || profile?.role !== 'superuser') {
|
|
edgeLogger.warn('Unauthorized backfill attempt', {
|
|
userId: context.user.id,
|
|
requestId: context.requestId
|
|
});
|
|
return new Response(
|
|
JSON.stringify({ error: 'Unauthorized: Superuser access required' }),
|
|
{ status: 403, headers: { ...corsHeadersWithMethods, 'Content-Type': 'application/json' } }
|
|
);
|
|
}
|
|
|
|
// Execute the backfill function
|
|
const { data, error } = await supabase.rpc('backfill_park_locations');
|
|
|
|
if (error) {
|
|
edgeLogger.error('Error running park location backfill', {
|
|
error,
|
|
requestId: context.requestId
|
|
});
|
|
throw error;
|
|
}
|
|
|
|
edgeLogger.info('Park location backfill completed', {
|
|
results: data,
|
|
requestId: context.requestId
|
|
});
|
|
|
|
return new Response(
|
|
JSON.stringify({
|
|
success: true,
|
|
...data,
|
|
}),
|
|
{ headers: { ...corsHeadersWithMethods, 'Content-Type': 'application/json' } }
|
|
);
|
|
}
|
|
);
|