认证

用户管理

查看、删除和导出用户信息。


您可以在Dashboard的用户页面查看用户信息。也可以通过表编辑器查看Auth模式下的所有数据表内容。

通过API访问用户数据

出于安全考虑,Auth模式不会在自动生成的API中暴露。如果您需要通过API访问用户数据,可以在public模式中创建自己的用户表。

请确保通过启用行级安全来保护该表。引用auth.users表以确保数据完整性,并在引用中指定on delete cascade。例如,一个public.profiles表可能如下所示:

1
2
3
4
5
6
7
8
9
create table public.profiles ( id uuid not null references auth.users on delete cascade, first_name text, last_name text, primary key (id));alter table public.profiles enable row level security;

要在每次用户注册时更新您的public.profiles表,可以设置一个触发器。如果触发器失败可能会阻止用户注册,因此请彻底测试您的代码。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
-- 向public.profiles插入一行create function public.handle_new_user()returns triggerlanguage plpgsqlsecurity definer set search_path = ''as $$begin insert into public.profiles (id, first_name, last_name) values (new.id, new.raw_user_meta_data ->> 'first_name', new.raw_user_meta_data ->> 'last_name'); return new;end;$$;-- 每次创建用户时触发该函数create trigger on_auth_user_created after insert on auth.users for each row execute procedure public.handle_new_user();

添加和获取用户元数据

您可以在用户注册时为其分配元数据:

1
2
3
4
5
6
7
8
9
10
const { , } = await ..({ : 'valid.email@supabase.io', : 'example-password', : { : { : 'John', : 27, }, },})

用户元数据存储在 auth.users 表的 raw_user_meta_data 列中。要查看元数据:

1
2
3
4
const { : { },} = await ..()let = ?.

删除用户

您可以直接删除用户,也可以通过管理控制台在"认证 > 用户"中进行删除。请注意,从auth.users表中删除用户不会自动注销该用户。由于Supabase使用JSON Web Tokens (JWT),用户的JWT在过期前将保持"有效"。如果您希望立即撤销用户的访问权限,请考虑使用如下所述的行级安全策略。

导出用户

由于Supabase构建在Postgres之上,您可以通过SQL编辑器选项卡查询auth.usersauth.identities表来提取所有用户:

1
select * from auth.users;

然后您可以将结果导出为CSV格式。