In the previous lesson, we wrote our first edge function and got familiar with the basic structure of an edge function. In this lesson, we will write edge functions for some complex use cases. We will start with a basic example and gradually move to complex ones.
Using Supabase client with authentication in Edge Function
In our first example, we will use supabase-js within the edge function to get data from our RLS enabled table. We assume that we have a users
table in our public
schema containing info of each user and has an RLS policy allowing users to access only their data.
We will write an edge function, which, if invoked by an authenticated user, will send that particular user’s info from the users
table.
We first import the modules we will need. Supabase recommends using esm.sh for importing Node.js modules that are published to npm. To do so, we can simply put https://esm.sh/
in front of the package name. Here, we are importing the supabase module using esm.
In our handler, we initialise the supabase client using the createClient
method. Supabase URL and the anon key are accessible by default in the env for edge function, and we can access them using Deno’s built-in Deno.env.get
method.