DEV Community

Cover image for Supabase automatically create user profiles on sign up
Chris Bongers
Chris Bongers

Posted on • Originally published at daily-dev-tips.com

3

Supabase automatically create user profiles on sign up

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.

Triggers and functions in Supabase

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.

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();
Enter fullscreen mode Exit fullscreen mode

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

Heroku

Built for developers, by developers.

Whether you're building a simple prototype or a business-critical product, Heroku's fully-managed platform gives you the simplest path to delivering apps quickly — using the tools and languages you already love!

Learn More

Top comments (0)

Neon image

Next.js applications: Set up a Neon project in seconds

If you're starting a new project, Neon has got your databases covered. No credit cards. No trials. No getting in your way.

Get started →

👋 Kindness is contagious

Dive into this insightful write-up, celebrated within the collaborative DEV Community. Developers at any stage are invited to contribute and elevate our shared skills.

A simple "thank you" can boost someone’s spirits—leave your kudos in the comments!

On DEV, exchanging ideas fuels progress and deepens our connections. If this post helped you, a brief note of thanks goes a long way.

Okay