1. 去 supabase.com 注册并创建项目
2. 项目设置 → API,复制 URL 和 anon key,替换文件开头 JS 里的 SUPABASE_URL 和 SUPABASE_ANON_KEY
3. 进入 SQL Editor,执行以下建表SQL:
create table diet_records (
id uuid primary key default gen_random_uuid(),
user_id uuid references auth.users not null,
date date not null,
carb_type text,
weight text, waist text,
breakfast text, lunch text, dinner text,
anaerobic text, aerobic text,
is_public boolean default false,
username text default '匿名用户',
created_at timestamptz default now()
);
alter table diet_records enable row level security;
create policy "用户可查看自己的记录" on diet_records for select using (auth.uid() = user_id);
create policy "所有人可查看公开记录" on diet_records for select using (is_public = true);
create policy "用户可新增自己的记录" on diet_records for insert with check (auth.uid() = user_id);
create policy "用户可修改自己的记录" on diet_records for update using (auth.uid() = user_id);
create policy "用户可删除自己的记录" on diet_records for delete using (auth.uid() = user_id);
-- 用户资料表(用户名/隐私设置)
create table if not exists user_profiles (
user_id uuid primary key references auth.users,
username text,
is_private boolean default true,
created_at timestamptz default now()
);
alter table user_profiles enable row level security;
create policy "用户可查看自己的资料" on user_profiles for select using (auth.uid() = user_id);
create policy "用户可修改自己的资料" on user_profiles for update using (auth.uid() = user_id);
create policy "用户可新增自己的资料" on user_profiles for insert with check (auth.uid() = user_id);
-- 注册后自动创建profile的触发器(可选)
create or replace function public.handle_new_user()
returns trigger as $$
begin
insert into public.user_profiles (user_id, username, is_private)
values (new.id, split_part(new.email, '@', 1), true);
return new;
end;
$$ language plpgsql security definer;
drop trigger if exists on_auth_user_created on auth.users;
create trigger on_auth_user_created
after insert on auth.users
for each row execute function public.handle_new_user();