2

How do i protect a Cloudflare Worker route to authorize only if the user is authenticated on Supabase?

I'm using Cloudflare Pages Function to create a worker inside the Cloudflare Pages: https://developers.cloudflare.com/pages/platform/functions/

import { createClient } from '@supabase/supabase-js'

export async function onRequest(context) {
   const token = context.request.headers.get('Cookie')
   const supabase = createClient(
      'url',
      'key')
   const {user, error} = await supabase.auth.getUser(token)
   if(user){
      return new Response("YES!")
   } else {
      return new Response("Access Denied")
   }
}

1 Answer 1

3

You can follow the same approach described in this post with some tweaks as it is not a Deno Edge Function.

const token = context.request.headers.get('Cookie')
const options = { global: { headers: { Authorization: 'Bearer '+token } } };
const supabaseClient = createClient('url', 'key', options);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.