I had a problem with Database of Supabase on Flutter.
I couldn't update the database.
I implemented the sign in feature with Google.
And I referenced this video: Flutter Database & User Authentication Quickstart
What should I do?
My code is
final userId = supabase.auth.currentUser!.id;
final username = _nameController.text.trim();
await supabase.from('profiles').update({
'username': username,
// the list has place name
'places': userPlacesList,
}).eq('id', userId);
}
My sql code is
create table profiles (
id uuid references auth.users not null primary key,
updated_at timestamp with time zone,
username text unique,
avatar_url text,
places text[],
constraint username_length check (char_length(username) >= 1)
);
alter table profiles
enable row level security;
create policy "Public profiles are viewable by everyone." on profiles
for select using (true);
create policy "Users can insert their own profile." on profiles
for insert with check (auth.uid() = id);
create policy "Users can update own profile." on profiles
for update using (auth.uid() = id);
create function public.handle_new_user()
returns trigger as $$
begin
insert into public.profiles (id, username, avatar_url)
values (new.id, new.raw_user_meta_data->>'username', new.raw_user_meta_data->>'avatar_url');
return new;
end;
$$ language plpgsql security definer;
create trigger on_auth_user_created
after insert on auth.users
for each row execute procedure public.handle_new_user();
These are photos;
Please help me...
What did I do:
I changed the Target roles of "Users can update own profile." from Default to authenticated.
But nothing happened.
I think that creating function was bad for me. I don't know it's needed.
I solved this problem by myself.
I changed my code:
final userId = supabase.auth.currentUser!.id;
final username = _nameController.text.trim();
await supabase.from('profiles').upsert({
'id': userId,
'username': username,
'places': userPlacesList,
});
I thought update function can make the data on database when the data isn't there.
But I noticed that if I don't have the data on database, update function won't add it.
So I changed to upsert from update.
Thank you for reading!