auth-js icon indicating copy to clipboard operation
auth-js copied to clipboard

(Proof-of-concept) Adding user metadata with signup

Open johncomposed opened this issue 4 years ago • 6 comments

What kind of change does this PR introduce?

Lets you add metadata when signing up a user instead of having to sign up then update.

What is the current behavior?

Adding arbitrary data to the auth.user table is only possible via the update call.

What is the new behavior?

You can optionally pass in user_metadata in the options object to add the metadata upon signup.

Additional context

I'm working on an app that has two types/levels of users, (e.g. teachers and students). The structure of user info stored for each is different enough that it makes more sense to have separate public.students and public.teachers tables. I was hoping to set up a trigger similar to the handle_new_users example described in the guide but right now when I call signup I don't actually know whether it's a student or a teacher so I don't know which table to make a new row in. So I'd have to do a post-signup update call or some sort of custom function, both of which just feel a little clunky.

Here's where it gets interesting. While the gotrue docs imply you can only pass {email, password}, the code itself (and its types) actually allow you to pass in {email, password, data}, setting the metadata upon signup.

I confirmed this works by monkeypatching it into the client in my project repo, so I forked this repo and edited the code in Github's inline editor for a little proof-of-concept PR here. If you actually want to pull it in I'd be happy to properly clone it and write some tests, but I figured I'd just share this for now.

Cheers!

johncomposed avatar May 26 '21 20:05 johncomposed

looked at doing this same thing and then stumbled on this PR, thanks for submitting this @johncomposed

this change seems awfully straight forward and low risk as it's just passing through the user metadata which is already supported in GoTrue, so, thoughts on what needs to happen to see this PR reviewed?

danalloway avatar Aug 24 '21 16:08 danalloway

Does this work? I don't think GoTrue supports metadata on signUp: https://github.com/supabase/gotrue#post-signup

what needs to happen to see this PR reviewed?

If we can add some tests that show it working I can merge this in 👍 (otherwise I'll dig into this next week myself)

kiwicopple avatar Aug 25 '21 11:08 kiwicopple

it's not documented well, but here is the spot in the signup flow where the metadata is automatically updated if the Data param is passed through: https://github.com/supabase/gotrue/blob/master/api/signup.go#L99-L101

danalloway avatar Aug 25 '21 13:08 danalloway

If we can add some tests that show it working I can merge this in 👍 (otherwise I'll dig into this next week myself)

👍🏼 thank you

danalloway avatar Aug 25 '21 13:08 danalloway

@kiwicopple changes made in #130 with tests

danalloway avatar Aug 30 '21 15:08 danalloway

Wow! This change https://github.com/supabase/gotrue-js/pull/130 would be very helpful. Thx @danalloway. Is there any chance to merge this request in the nearest future and update the supabase libraries, @kiwicopple?

Inzeppelin avatar Sep 05 '21 16:09 Inzeppelin

hey everyone, gonna be closing this PR since it is already possible to include user_metadata in the signup method.

here's how to do it in the latest master version / rc (soon to be supabase-js/v2):

  1. master
  2. rc

kangmingtay avatar Sep 27 '22 07:09 kangmingtay

also, thanks everyone for contributing in one way or another and @johncomposed for taking the time to work on this PR

kangmingtay avatar Sep 27 '22 07:09 kangmingtay