DEV Community

Creating User Profiles on Sign-Up in Supabase

Sebastian Ruhleder on August 11, 2021

Supabase stores authentication-related information (unique ID, email, password, etc.) in the auth.users table when users sign up. The auth schema i...
Collapse
 
sinabyr profile image
Sina Beyraghdar

Hello Sebastian!
Thank you for making this great and useful article, I'm trying to add email address as the username whenever a user record is created, but I don't clearly know how to implement that. Is there any way to achieve that?

Collapse
 
sruhleder profile image
Sebastian Ruhleder

Hi Sina,

Sorry for the late reply!

First, you should add an email column to the profile table:

CREATE TABLE public.profile (
  id UUID REFERENCES auth.users NOT NULL,
  display_name TEXT NULL,
  email TEXT NOT NULL,
  PRIMARY KEY (id)
);
Enter fullscreen mode Exit fullscreen mode

Then, you can access the email column from the auth.users table to insert the right value:

CREATE FUNCTION
  public.create_profile_for_new_user()
  RETURNS TRIGGER AS
  $$
  BEGIN
    INSERT INTO public.profile (id, email)
    VALUES (NEW.id, NEW.email);
    RETURN NEW;
  END;
  $$ LANGUAGE plpgsql SECURITY DEFINER;
Enter fullscreen mode Exit fullscreen mode

The function create_profile_for_new_user is used as an AFTER INSERT trigger for the auth.users table. So NEW within this function refers to the new row inserted into the auth.users table and thus allows you to access every column of it.

Hope this helps! :)

Collapse
 
hunterbecton profile image
Hunter Becton

Thanks for sharing how to save data from a Third-Party provider! Very helpful!

Collapse
 
__junaidshah profile image
Junaid

It was really helpful , thankyou

Collapse
 
dailydevtips1 profile image
Chris Bongers • Edited

Hi Sebastian!

Thanks for this amazing article, trying this as well. but hitting a error when even using the plain ID only function.

I'm using GitHub as my login, but I always get redirect with:
http://localhost:3000/?error=server_error&error_description=Database+error+saving+new+user

Is there any way to see what would be wrong?

-- Edit
Managed to get it working with the following:

begin
  insert into public.profiles (id)
  values (new.id);
  return new;
end;
Enter fullscreen mode Exit fullscreen mode
Collapse
 
sruhleder profile image
Sebastian Ruhleder

Hi Chris!

I assume the use of:

NEW.raw_user_meta_data ->> 'user_name'
Enter fullscreen mode Exit fullscreen mode

doesn't work with GitHub?! πŸ€”

The following PR has introduced this feature: github.com/supabase/gotrue/pull/127

I haven't had time to look deeper into this, but the gotrue repo of supabase should be the right place to investigate (or open an issue).

Glad you could fix it and you enjoyed the article!

Collapse
 
dplastaras profile image
dimitris plastaras

Hello Sebastian.
Thank's for sharing your knowledge with all of us..
Your article help me so much.!!

Can you help me with this:

When the user sign up...
How can you check if the email exists in auth.users table?

Collapse
 
sruhleder profile image
Sebastian Ruhleder

Hi Dimitris!

The supabase.auth.signUp function automatically checks if a user already exists. (See supabase.com/docs/reference/javasc... for further info.)

Is that what you mean?

Best regards,
Sebastian

Collapse
 
james0r profile image
James Auble

I'm using standard email and password sign up and passing data property in the second argument of supabase.auth.signUp(). I see this in the docs but I see no mentions of how to access it or view it in the admin.

How can I go about inserting meta data from signUp() into my profile table?

Thanks!

Collapse
 
canrau profile image
Can Rau

Hey, hope you've figured it out by now, still wanted to add this for future reference

This should work using

begin
  insert into public.profile(id, account_id)
  values(new.id, (new.raw_user_meta_data->>'account_id')::uuid);

  return new;
end;
Enter fullscreen mode Exit fullscreen mode

which I found in Discord.

Collapse
 
thesnowmanndev profile image
Kyle Martin

Thanks for sharing this article. I was having an issue and I believe this will solve it. I appreciate it.

Collapse
 
g5l profile image
Gabriel Debona

Really helpful article, thank you!!

Collapse
 
supraja5143 profile image
supraja5143 • Edited

hi @sruhleder I am getting this error while sign up in supabase i have taken the above steps still i am getting this error AuthApiError: duplicate key value violates unique constraint "user_pkey"