Secure Supabase Email Sign-In for iOS Apps
Résumé
Pixel shares a guide to setting up Supabase email sign-in and a per-user profiles table in an iOS app. It covers the SQL schema, Swift implementation, Row Level Security policies, and common security mistakes, including trusting a public app key or allowing updates to unrestricted columns.
DEV Community
Supabase email sign-in and a profiles table for iOS, done safely
Almost every iOS app I start needs the same first hour of work: email sign-in, a place to keep per-user data, and a way to make sure users can only touch their own rows. Supabase gives you most of it. The part people get wrong is the profiles table, because the obvious version is also the insecure one. This post walks through the setup I use: the SQL, the Swift, and the three mistakes to avoid. It assumes you know basic SwiftUI and have a free Supabase project. The model: two tables, one id Supabase Auth stores credentials in auth.users. You don't add your own columns there. Instead you create a public.profiles table whose primary key is the auth user's id: create table if not exists public.profiles (
id uuid primary key references auth.users (id) on delete cascade,
email text,
full_name text,
avatar_url text,
created_at timestamptz not null default now(),
updated_at timestamptz not null default now()
);
on delete cascade matters: when the auth user is deleted, the profile goes with it. No orphans. Mistake 1: forgetting that your key ships inside the app The key in your app is public. Anyone can extract it. So the key is not your security. Row Level Security (RLS) is. alter table public.profiles enable row level security;
-
- signed-out clients get nothing
revoke all on public.profiles from anon;
- signed-out clients get nothing
create policy "Users can read their own profile"
on public.profiles for select
to authenticated
using ((select auth.uid()) = id);
create policy "Users can update their own profile"
on public.profiles for update
to authenticated
using ((select auth.uid()) = id)
with check ((select auth.uid()) = id);
With RLS on and no matching policy, a request just sees zero rows. Wrapping auth.uid() in (select ...) lets Postgres evaluate it once per query instead of once per row. Mistake 2: letting users update any column A row-level policy says which rows a user can change. It says nothing about which columns. If you later add a subscription_status column, the policy above would let a user set it to active from the client. Fix it with a column-level grant: revoke all on public.profiles from authenticated;
grant select on public.profiles to authenticated;
grant update (full_name, avatar_url) on public.profiles to authenticated;
Now the app can edit exactly two columns. Anything else (billing state, roles, flags) can only be written by your backend with the service role. When you add a user-editable column, you add it to that list on purpose. Mistake 3: creating the profile from the app If the app inserts the profile row after sign-up, you'll eventually have a user with no profile: the network dropped, or the app was killed between the two calls. Let the database do it, atomically, with a trigger: create or replace function public.handle_new_user()
returns trigger
language plpgsql
security definer
set search_path = ''
as $$
begin
insert into public.profiles (id, email, full_name)
values (
new.id,
new.email,
coalesce(
nullif(new.raw_user_meta_data ->> 'full_name', ''),
nullif(new.raw_user_meta_data ->> 'name', '')
)
)
on conflict (id) do nothing;
return new;
end;
$$;
create trigger on_auth_user_created
after insert on auth.users
for each row execute function public.handle_new_user();
revoke execute on function public.handle_new_user() from public, anon, authenticated;
Three details worth copying: security definer lets the function write to profiles even though no user is signed in yet. Because it's powerful, set search_path = '' stops anyone from hijacking name resolution. The last line removes the ability to call the function through the API. It should only fire from the trigger. Metadata passed at sign-up (full_name) is copied into the profile in the same transaction. Run all of this in SQL Editor > New query. Every statement above can be re-run safely if you add if not exists / drop ....

Powered by Readybot.io