1

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")
   }
}
Joao Gui
  • 119
  • 6

1 Answers1

2

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);
Mansueli
  • 6,223
  • 8
  • 33
  • 57