Supabase sign-in, a profiles table with an AI-edit balance, and spending it safely on the server.
AI calls cost money, so each account gets a balance of AI edits. Every edit spends one. The balance lives in the database and is only ever changed by server code.
supabase/migrations/001_profiles.sql
create table public.profiles (
id uuid primary key references auth.users(id) on delete cascade,
token_balance integer not null default 12
);
alter table public.profiles enable row level security;
create policy "read own profile" on public.profiles for select using ((select auth.uid()) = id);
-- Spend atomically: never below zero, even with two requests at once
create function public.spend_token(p_user uuid) returns boolean
language sql security definer set search_path = '' as $$
update public.profiles set token_balance = token_balance - 1
where id = p_user and token_balance > 0
returning true;
$$;
revoke all on function public.spend_token(uuid) from public, anon, authenticated;
This lesson comes with a Velour plan
Every Academy lesson, code sample and Claude Code prompt is included with Publish and Growth, along with everything in the builder.