supabase / supabase-js

An isomorphic Javascript client for Supabase. Query your Supabase database, subscribe to realtime events, upload and download files, browse typescript examples, invoke postgres functions via rpc, invoke supabase edge functions, query pgvector.
https://supabase.com
MIT License
2.86k stars 220 forks source link

Update user data #23

Closed zlwaterfield closed 3 years ago

zlwaterfield commented 3 years ago

Question

How can you update user data? Here are the possibilities I see:

  1. I see there is user_metadata but don't see a way to update it.
  2. I could add a table called user and join on the UID the signup generates. If that is the case how can we create a foreign key to the auth'd user row?
hansy commented 3 years ago

@zlwaterfield Check out Issue #21. In short, you can update user info by directly sending a request to your supabase endpoint. Supabase is using Netlify's GoTrue API to power authentication so you should be able to call any endpoint listed there using your unique supabase url and API key.

zlwaterfield commented 3 years ago

Awesome thanks, I'll take a look.

kiwicopple commented 3 years ago

thanks for pitching in here @hansy

zlwaterfield commented 3 years ago

For anyone else coming here, here is an example snippet to help:

  async changeEmail(email) {
    return await axios({
      method: 'PUT',
      url: `${SupabaseService.auth.authUrl}/user`,
      headers: {
        authorization: `Bearer ${SupabaseService.auth.accessToken}`,
        apikey: SupabaseService.auth.supabaseKey,
      },
      data: {
        email,
      },
    });
  }
kiwicopple commented 3 years ago

Also a note that we will be adding this to supabase-js - we are revising the auth API now

tconroy commented 2 years ago

Now that it's 2021, curious what the preferred solution is.

there is

await supabase.auth.update({
  data: {
    email: 'foo@bar.com'
  },
});

however, this doesn't seem to correlate with the users.email column.

Also, not clear how to re-trigger a confirmation email on email change.

thorwebdev commented 2 years ago

@tconroy for email it's const { user, error } = await supabase.auth.update({email: 'new@email.com'})

This will send an email to both the user's current and new email with a confirmation link.

raimille1 commented 2 years ago

Any idea on how to restrict updates to that column for specific values?

// I want to throw an error for when a user does this, but let them update anything else
await supabase.auth.update({
  data: {
    read_only_property: true
  },
})
rommyarb commented 1 year ago

It's 2022, boys. Now you can do these:

ankushg commented 1 year ago

@rommyarb it's still 2022, but it looks like the supabase.auth.api.updateUser method doesn't exist anymore in version 2.0+ :(

Is there an updated way to do this client-side with JWT?

thorwebdev commented 1 year ago

You can update a logged in user client-side by calling: supabase.auth.updateUser: https://supabase.com/docs/reference/javascript/auth-updateuser

AwesomeZaidi commented 1 year ago

Once this update happens on the client, if im updating metadata, the user object in session doesn't reflect the change

tiagopaespride commented 1 year ago

Once this update happens on the client, if im updating metadata, the user object in session doesn't reflect the change

I had the same problem. It works if you refreshes the session after updating the user data.

await supabase.auth.refreshSession();

aldrinjenson commented 9 months ago

@rommyarb it's still 2022, but it looks like the supabase.auth.api.updateUser method doesn't exist anymore in version 2.0+ :(

Hey, this can now be done from server side using this API: supabase.auth.admin.updateUserById(userId, { email: 'foo@bar.com' });

leocastroz commented 4 months ago

In vue.js 3

import { supabase } from "@/clients/supabase";

const updateFirstName = async () => {
  try {
    const { data, error } = await supabase.auth.admin.updateUserById(userId, {
      user_metadata: { first_name: firstName.value }
    })
    if (error) {
      console.error('Error updating first name:', error);
    } else {
      console.log('First name updated successfully:', data);
    }
  } catch (error) {
    console.error('Error updating first name:', error);
  }
}

Let's go up Brazil!!

br