Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Invalid avatar_url being put into the public.profiles table #14

Open
avallete opened this issue Mar 12, 2024 · 0 comments · May be fixed by #15
Open

Invalid avatar_url being put into the public.profiles table #14

avallete opened this issue Mar 12, 2024 · 0 comments · May be fixed by #15

Comments

@avallete
Copy link

avallete commented Mar 12, 2024

Hey there !

Following this tutorial I've noticed something at the point where I was styling it to show the avatars and so on.

The "trigger" to automatically create new public.profile for every new auth.users is defined like so:

create function public.create_profile_for_user()
returns trigger
language plpgsql
security definer set search_path = public
as $$
begin
  insert into public.profiles (id, name, username, avatar_url)
  values (
    new.id,
    new.raw_user_meta_data->'name',
    new.raw_user_meta_data->'user_name',
    new.raw_user_meta_data->'avatar_url'
  );
  return new;
end;
$$;

When later on, in the app, we use the "avatar_url" in two different ways, the first one, is inside NewTweet component, to which we pass the user coming from our session then we do user.user_metadata.avatar_url and this display the user avatar just fine.

But we also show the avatars on the side of each tweet, but there, the source of the avatar_url is no longer the session but the profile table.

And in that case, next/image complain that the image url is not a valid one, because the actual content in the profile.avatar_url is "https://githubcontent/avatar/..." (notice the extra quotes).

This is because, we are using the "json representation" of the "avatar_url" value in our trigger instead of the actual string value.

Changing the trigger function in favor of this fixed the issue:

create function public.create_profile_for_user()
returns trigger
language plpgsql
security definer set search_path = public
as $$
begin
  insert into public.profiles (id, name, username, avatar_url)
  values (
    new.id,
    -- We want to access the value with ->> operator
    new.raw_user_meta_data->>'name',
    new.raw_user_meta_data->>'user_name',
    new.raw_user_meta_data->>'avatar_url'
  );
  return new;
end;
$$;

I've opened a PR to fix the concerned README.md. Let me know if I can be of any other help.

avallete added a commit to avallete/build-a-twitter-clone-with-the-next.js-app-router-and-supabase that referenced this issue Mar 12, 2024
@avallete avallete linked a pull request Mar 12, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
1 participant