<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Faris Perwira</title>
    <description>The latest articles on DEV Community by Faris Perwira (@faris_perwira).</description>
    <link>https://dev.to/faris_perwira</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3312453%2Ff19a7e38-f6c4-45fe-9f66-0ddd4ed7b958.jpg</url>
      <title>DEV Community: Faris Perwira</title>
      <link>https://dev.to/faris_perwira</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/faris_perwira"/>
    <language>en</language>
    <item>
      <title>Extending Supabase Auth Users Table</title>
      <dc:creator>Faris Perwira</dc:creator>
      <pubDate>Mon, 07 Jul 2025 11:36:41 +0000</pubDate>
      <link>https://dev.to/faris_perwira/extending-supabase-auth-users-table-433p</link>
      <guid>https://dev.to/faris_perwira/extending-supabase-auth-users-table-433p</guid>
      <description>&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffuhft197yvqncxla9ta7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffuhft197yvqncxla9ta7.png" alt="Supabase logo" width="800" height="336"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Recently, I worked on a project that uses Supabase, and it has a Profile feature for users that required me to extend Supabase’s &lt;code&gt;auth.users&lt;/code&gt; table. Extending means creating a new table that references the &lt;code&gt;auth.users&lt;/code&gt; table. Why would we need to extend the &lt;code&gt;auth.users&lt;/code&gt; table? Well… because Supabase doesn’t allow us to add new columns to the &lt;code&gt;auth.users&lt;/code&gt; table.&lt;/p&gt;

&lt;h2&gt;
  
  
  Extending
&lt;/h2&gt;

&lt;p&gt;Here’s how we extend the &lt;code&gt;auth.users&lt;/code&gt; table. First, we need to create a new table to store the additional user data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TYPE "public"."user_profile_role" AS ENUM('admin', 'user');
CREATE TABLE "user_profiles" (
 "id" uuid PRIMARY KEY NOT NULL REFERENCES auth.users ON DELETE CASCADE,
 "role" "user_profile_role",
 "first_name" varchar(255),
 "last_name" varchar(255),
 "created_at" timestamp DEFAULT now() NOT NULL,
 "updated_at" timestamp DEFAULT now() NOT NULL
);
ALTER TABLE "user_profiles" ENABLE ROW LEVEL SECURITY
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Second, we need to create a trigger that will execute a function responsible for creating a &lt;code&gt;user_profiles&lt;/code&gt; record whenever a new user is created.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE FUNCTION public.handle_new_user()
RETURNS TRIGGER
SET search_path = ''
AS $$
BEGIN
    INSERT INTO public.user_profiles (id, role, first_name, last_name)
    VALUES (
      new.id, 
      CAST(new.raw_user_meta_data-&amp;gt;&amp;gt;'role' AS public.user_profile_role), 
      new.raw_user_meta_data-&amp;gt;&amp;gt;'first_name', 
      new.raw_user_meta_data-&amp;gt;&amp;gt;'last_name'
    );
    RETURN new;
END;

$$ LANGUAGE plpgsql security definer;
CREATE TRIGGER on_auth_user_created
  AFTER INSERT ON auth.users
  FOR EACH ROW EXECUTE PROCEDURE public.handle_new_user();
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;code&gt;raw_user_meta_data&lt;/code&gt; is metadata that we send when calling Supabase’s signup function. Below is an example of how we can call the signup function:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;const ROLE = {
  ADMIN: "admin",
  USER: "user",
};

const { error } = await supabase.auth.signUp({
  email,
  password,
  options: {
    data: {
      role: ROLE.USER,
      first_name: "Faris",
      last_name: "Perwira"
    },
    emailRedirectTo: `${currentDomain}/auth/confirm`,
  },
});
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  What’s Next?
&lt;/h2&gt;

&lt;p&gt;You might consider implementing the &lt;a href="https://supabase.com/docs/guides/database/postgres/row-level-security" rel="noopener noreferrer"&gt;RLS definitions&lt;/a&gt; for &lt;code&gt;user_profiles&lt;/code&gt; table, which will make the table more secure. Writing all the SQL scripts for RLS definitions and table creation can be a lot of extra work. You might consider using an ORM like &lt;a href="https://orm.drizzle.team/docs/tutorials/drizzle-with-supabase" rel="noopener noreferrer"&gt;Drizzle&lt;/a&gt; to generate the SQL scripts based on your schema.&lt;/p&gt;

&lt;p&gt;References:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Supabase — Managing User Data (&lt;a href="https://supabase.com/docs/guides/auth/managing-user-data?queryGroups=language&amp;amp;language=js" rel="noopener noreferrer"&gt;https://supabase.com/docs/guides/auth/managing-user-data?queryGroups=language&amp;amp;language=js&lt;/a&gt;)&lt;/li&gt;
&lt;/ul&gt;

</description>
      <category>supabase</category>
      <category>postgres</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
