Supabase automatically create user profiles on sign up

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

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 give us a username and profile image already.
Let's take our existing GitHub login as an example and see how to automate the profile creation.
The cool part about Supabase is that its Postgres based, and Postgres has this super cool feature called "Triggers".
This means you can set a trigger for a specific action on 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.

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.

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.

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 my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter