Skip to content

The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.

chore: add cors to existing example

thorwebdev26d541082269
diff --git a/examples/edge-functions/supabase/functions/select-from-table-with-auth-rls/_utils/cors.ts b/examples/edge-functions/supabase/functions/select-from-table-with-auth-rls/_utils/cors.ts
new file mode 100644
index 00000000000..32196801f9c
--- /dev/null
+++ b/examples/edge-functions/supabase/functions/select-from-table-with-auth-rls/_utils/cors.ts
@@ -0,0 +1,7 @@
+export const corsHeaders = {
+ 'Access-Control-Allow-Origin': '*',
+ 'Access-Control-Allow-Methods': 'POST',
+ 'Access-Control-Expose-Headers': 'Content-Length, X-JSON',
+ 'Access-Control-Allow-Headers':
+ 'apikey, X-Client-Info, Content-Type, Authorization, Accept, Accept-Language, X-Authorization',
+}
diff --git a/examples/edge-functions/supabase/functions/select-from-table-with-auth-rls/index.ts b/examples/edge-functions/supabase/functions/select-from-table-with-auth-rls/index.ts
index f11a50e63e6..a8752740cf3 100644
--- a/examples/edge-functions/supabase/functions/select-from-table-with-auth-rls/index.ts
+++ b/examples/edge-functions/supabase/functions/select-from-table-with-auth-rls/index.ts
@@ -4,6 +4,7 @@
import { serve } from 'https://deno.land/[email protected]/http/server.ts'
import { createClient } from 'https://esm.sh/@supabase/supabase-js@^1.33.2'
+import { corsHeaders } from './_utils/cors.ts'
const supabase = createClient(
// Supabase API URL - env var exported by default when deployed.
@@ -15,15 +16,19 @@ const supabase = createClient(
console.log(`Function "select-from-table-with-auth-rls" up and running!`)
serve(async (req: Request) => {
+ // This is needed if you're planning to invoke your function from a browser.
+ if (req.method === 'OPTIONS') {
+ return new Response('ok', { headers: corsHeaders })
+ }
// Set the Auth context of the user that called the function.
// This way your row-level-security (RLS) policies are applied.
- supabase.auth.setAuth(req.headers.get('Authorization')!.split('Bearer ')[1])
+ supabase.auth.setAuth(req.headers.get('Authorization')!.replace('Bearer ', ''))
const { data, error } = await supabase.from('users').select('*')
console.log({ data, error })
return new Response(JSON.stringify({ data, error }), {
- headers: { 'Content-Type': 'application/json' },
+ headers: { ...corsHeaders, 'Content-Type': 'application/json' },
})
})