Subscribe

Supabase automatically create user profiles on sign up

✍️

How can we automatically create a user profile on sign up using Supabase

12 Dec, 2021 · 2 min read

We introduced a social login to our Supabase login system. It’s actually possible to automate the profile creation.

This is super cool, as most social providers already give us a username and profile image.

Let’s take our existing GitHub login as an example and see how to automate the profile creation.

Triggers and functions in Supabase

The cool part about Supabase is that it’s Postgres based, and Postgres has this super cool feature called “Triggers”.

This means you can set a trigger for a specific action which action should happen.

Mix that with Supabase functions, and we can trigger a function to create a profile on user creation. ✨

You can create these triggers and functions through the interface, but the easiest way is to run a SQL query.

Supabase SQL interface

Open the query interface and run the following one.

-- inserts a row into public.users
create function public.handle_new_user()
returns trigger
language plpgsql
security definer set search_path = public
as $$
begin
  insert into public.profiles (id, username, avatar_url)
  values (new.id, new.raw_user_meta_data ->> 'user_name', new.raw_user_meta_data ->> 'avatar_url');
  return new;
end;
$$;

-- trigger the function every time a user is created
create trigger on_auth_user_created
  after insert on auth.users
  for each row execute procedure public.handle_new_user();

What we do here is create a new function called handle_new_user.

This function states that it should insert on the public.profiles table and add id, username, and avatar_url. It takes the values from the new object, which refers to the item invoking this, which will be the auth.users one.

And then, we add the trigger which binds after each insert on the auth.users table to execute the function we just made.

Once you run this query, you can find them in your Supabase account under the database options.

Supabase Triggers and Functions

I’ve modified my own started template to auto show the image on signup, and you can see this now gets pulled from the login.

Enriched profile in Supabase

I found this super helpful, as it allows us to handle this on the database side and doesn’t include new code for our application.

You can also use these functions and triggers for other purposes. Maybe you wish to update a count or invoke an external action.

What would you use them for?

Thank you for reading, and let’s connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

Spread the knowledge with fellow developers on Twitter
Tweet this tip
Powered by Webmentions - Learn more

Read next 📖

Next portfolio - Filter by category

30 Nov, 2022 · 5 min read

Next portfolio - Filter by category

A glance at Turbopack

17 Nov, 2022 · 3 min read

A glance at Turbopack

Join 2097 devs and subscribe to my newsletter