How do I pass user_metadata when inviting a user? ...
# help
n
How do I pass user_metadata when inviting a user? Following the docs I get to the following statement however it appears that the metadata isn't set:
supabaseClient.auth.api.inviteUserByEmail('john.doe@example.com', {data: {first_name: 'John'}})
n
Hello @Nin! This thread has been automatically created from your message in #843999948717555735 a few seconds ago. We have already mentioned the @User so that they can see your message and help you as soon as possible! Want to unsubscribe from this thread? Right-click the thread in Discord (or use the ``...`` menu) and select "Leave Thread" to unsubscribe from future updates. Want to change the title? Use the ``/title`` command! We have solved your problem? Click the button below to archive it.
s
This is exactly how I'm doing it in my waiting-list app and it works https://github.com/silentworks/waiting-list/blob/main/src/routes/api/invite.json.js#L13-L20
How are your retrieving the metadata in your trigger function?
n
I'm not, I'm just logging the result and I don't see it in there :/ let me check again
s
When you say logging the results what do you mean by that?
n
This is in an Edge Function I have running locally, this is the part of the code that does the invite:
Copy code
const sendInvite = payload.invites.map((invite: any)=> supabaseClient.auth.api.inviteUserByEmail(invite.invite_email, {data: {first_name: 'John'}}))
    await Promise.all(sendInvite).then(console.log).catch(err => new Error('Failed to invite user'))
s
I just added a console log to my program and its logging that data out, there is a data object with a
user_metadata
object it in with the additional data. Also you might want to write that function differently because a error might be returned instead of data, your
.catch
won't catch this error at all because you need to check if the error object that is returned from the
inviteUserByEmail
function is empty manually.
n
Yeah so I have it logging as well but it's not logging the desired value in user_metadata
Copy code
{
    data: {
      id: "595a3bde-04bb-42e3-9d8d-a3faa5b435cf",
      aud: "authenticated",
      role: "authenticated",
      email: "john.doe@example.com",
      invited_at: "2022-06-19T21:37:29.452563112Z",
      phone: "",
      confirmation_sent_at: "2022-06-19T21:37:29.452563112Z",
      app_metadata: { provider: "email", providers: [Array] },
      user_metadata: {},
      identities: [],
      created_at: "2022-06-19T15:36:01.975444Z",
      updated_at: "2022-06-19T21:37:29.454257Z"
    },
    error: null
  }
And I'll look into that, thanks
s
If there was an error I would have said its due to a trigger failing, but there is no error
n
The only difference I see is that I'm not passing a redirectTo.. ¯\_(ツ)_/¯
s
Yeah that shouldn't make a difference
n
Should that be a URL?
s
Yeah that's a full URL, but you shouldn't need it in your case as its a optional parameter
Are you using your service key with the
supabaseClient
?
n
oh my god
Yeah I am
The issue was that the user already had a pending invite
s
LMAO
n
I was just spamming the same email address
s
I'm happy you figured it out
n
Ok, at least that's good to know. If there is a pending invite for the same email, it doesn't accept new user_metadata
s
I guess it wouldn't since the record would have existed already, it would just re-send the invite email.
I'm using that feature in my app as a means to nudge a user that I've invited already.
n
Yeah so in my case, I'm setting the tenant_id to which the user is going to be invited
Or no actually, I'm setting the user id by whom the user is being invited
Would you advise me to delete the user prior in case it exists?
s
If that matches your use case and is the behaviour you are after then yes
n
Meh sounds like an edge case I shouldn't worry about right now
s
I'm going to sign off for the night, but if you have anymore questions ask. I'm sure someone will answer or I'll have a look in the morning.
n
What I'll do is I'll block sending the invite in case the user already has a pending invite and create a new function to resend the same invite
Thanks for your help (and GitHub links) @silentworks!
And yeah, good night 🙂
s
Good night to you too.
n
Thread was archived by @Nin. Anyone can send a message to unarchive it.