The Postgres development platform. Supabase gives you a dedicated Postgres database to build your web, mobile, and AI applications.
TypeScript aialternativeauthdatabasedenoembeddingsexamplefirebasenextjsoauth2pgvectorpostgispostgrespostgresqlpostgrestrealtimesupabasevectorswebsockets
Adds a guide for managing user data
Paul Copplestone6b13dc50bf0a
diff --git a/web/docs/guides/auth.mdx b/web/docs/guides/auth.mdxindex c5fda7bd87f..b374d7c76af 100644--- a/web/docs/guides/auth.mdx+++ b/web/docs/guides/auth.mdx@@ -325,30 +325,12 @@ Navigate to Authentication > Settings on [app.supabase.io](https://app.supabase.Supabase provides special "Service" keys, which can be used to bypass all RLS.These should never be used in the browser or exposed to customers, but they are useful for administrative tasks.-### Create a `public.users` table+### Managing User Data-Even though Supabase provides an `auth.users` table, it is helpful to also create a users table in the `public` schema, which uses the same UID Primary Key as the `auth.users`.-For security purposes, the `auth` schema is not exposed on the auto-generated API. Creating a `public.users` table allows you to interact via the Supabase client –-especially useful for cross-table queries.+For security purposes, the `auth` schema is not exposed on the auto-generated API.+Even though Supabase provides an `auth.users` table, it can be helpful to create tables in the `public` schema for storing user data.-Pro tip: If you want to add a row to your `public.users` table every time a user signs up, you can use triggers. For example:--```sql--- inserts a row into public.users-create function public.handle_new_user()-returns trigger as $$-begin- insert into public.users (id)- values (new.id);- return new;-end;-$$ language plpgsql security definer;---- trigger the function every time a user is created-create trigger on_auth_user_created- after insert on auth.users- for each row execute procedure public.handle_new_user();-```+You can read more about this in the [Managing User Data](/docs/guides/auth/managing-user-data) guide.### Disable realtime for private tablesdiff --git a/web/docs/guides/auth/managing-user-data.mdx b/web/docs/guides/auth/managing-user-data.mdxnew file mode 100644index 00000000000..c75253aa34d--- /dev/null+++ b/web/docs/guides/auth/managing-user-data.mdx@@ -0,0 +1,81 @@+---+id: managing-user-data+title: Managing User Data+description: Securing your user data with Row Level Security.+---++For security purposes, the `auth` schema is not exposed on the auto-generated API.+Even though Supabase provides an `auth.users` table, it can be helpful to create tables in the `public` schema for storing user data.++## Creating user tables++When you create user tables, it's helpful to reference the `auth.users` table in the primary key. This ensures data integrity.+For example, a `public.profiles` table which is accessible via the API might look something like this:++```sql+create table public.profiles (+ id uuid references auth.users not null,+ updated_at timestamp with time zone,+ username text unique,+ avatar_url text,+ website text,++ primary key (id),+ unique(username),+ constraint username_length check (char_length(username) >= 3)+);++alter table public.profiles enable row level security;+```+++## Public access++Since Row Level Security is enabled, this table is accessible via the API but no data will be returned unless we set up some Policies.+If we wanted the data to be _readable_ by everyone but only allow logged-in user to update their own data, the Policies would look like this:++```sql+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 );+```++## Private access++If we didn't want this data to be publicly accessible, and instead it should only be _readable_ by the user who owns the data, we just need to change the `for select` query above.++```sql+create policy "Profiles are viewable by users who created them."+ on profiles for select+ using ( auth.uid() = id );+```++The nice thing about this pattern? We can now query this table via the API and we don't need to include data filters in our API queries - the Policies will handle that for us:++```js+// This will return nothing while the user is logged out+const { data: profile, error } = supabase+ .from('profiles')+ .select('id, username, avatar_url, website')++// After the user is logged in, this will only return+// the logged-in user's data - in this case a single row+const { error } = await supabase.auth.signIn({ email })+const { data: profile, error } = supabase+ .from('profiles')+ .select('id, username, avatar_url, website')+```++## Bypassing Row Level Security++If you need to fetch a full list of user profiles, we supply a `service_key` which you can use with your API and Client Libraries.++Make sure you _NEVER_ expose this publicly. But it can be used on the server-side to fetch all of the profiles.\ No newline at end of filediff --git a/web/sidebars.js b/web/sidebars.jsindex a98086eea32..d9de61e1d20 100755--- a/web/sidebars.js+++ b/web/sidebars.js@@ -134,13 +134,21 @@ module.exports = {label: 'Auth',collapsed: false,items: [- 'guides/auth/auth-apple',- 'guides/auth/auth-bitbucket',- 'guides/auth/auth-facebook',- 'guides/auth/auth-github',- 'guides/auth/auth-gitlab',- 'guides/auth/auth-google',- 'guides/auth/auth-twitter',+ 'guides/auth/managing-user-data',+ {+ type: 'category',+ label: 'OAuth Providers',+ collapsed: false,+ items: [+ 'guides/auth/auth-apple',+ 'guides/auth/auth-bitbucket',+ 'guides/auth/auth-facebook',+ 'guides/auth/auth-github',+ 'guides/auth/auth-gitlab',+ 'guides/auth/auth-google',+ 'guides/auth/auth-twitter',+ ],+ },],},// {