Search code examples
supabasesupabase-databasesupabase-js

Supabase Auth: Accessing Provider Information in Supabase's new Response Object


I have set up a Supabase table, function, and trigger to handle new user creations. However, I am only able to access the provider_id from the new object in the trigger function. I would like to know if there is a way to access the entire provider information, such as google, discord, or other providers, instead of just the provider_id.

Supabase Table and Trigger Setup:

-- Table
create table users (
    id uuid references auth.users not null primary key,
    email text null,
    avatar text null,
    username text null,
    fullname text null,
    provider json null,
    provider_id text null,
    constraint users_pkey primary key (id),
    constraint users_id_fkey foreign key (id) references auth.users (id)
);

-- Function
create or replace function public.handle_new_user() 
returns trigger as $$
BEGIN
  INSERT INTO public.users (
    id,
    email,
    avatar,
    username,
    fullname,
    provider,
    provider_id
  )
  VALUES (
    new.id,
    new.email,
    jsonb_extract_path_text(new.raw_user_meta_data, 'avatar_url'),
    jsonb_extract_path_text(new.raw_user_meta_data, 'name'),
    jsonb_extract_path_text(new.raw_user_meta_data, 'full_name'),
    new.raw_user_meta_data,
    jsonb_extract_path_text(new.raw_user_meta_data, 'provider_id')
  );
  RETURN new;
END;
$$ language plpgsql security definer;

-- Trigger
create trigger on_auth_user_created
  after insert on auth.users
  for each row execute procedure public.handle_new_user();

Supabase Output: When using the useUser() function from @supabase/auth-helpers-react in my code and logging the user object, I receive the following output: json session output


Solution

  • This issue/problem is already solved, Found out it is located at raw_app_meta_data it is similar to raw_user_meta_data that i have used.

    So here's the updated function that I did:

    BEGIN
      -- Insert the values into the public.users table
      INSERT INTO public.users (
        id,
        email,
        avatar,
        username,
        fullname,
        provider,
        provider_id
      )
      VALUES (
        new.id,
        new.email,
        jsonb_extract_path_text(new.raw_user_meta_data, 'avatar_url'),
        jsonb_extract_path_text(new.raw_user_meta_data, 'name'),
        jsonb_extract_path_text(new.raw_user_meta_data, 'full_name'),
        jsonb_extract_path_text(new.raw_app_meta_data, 'provider'),
        jsonb_extract_path_text(new.raw_user_meta_data, 'provider_id')
      );
      RETURN new;
    END;