Skip to content
Auth

用户

在 Supabase Auth 中,用户是指拥有用户 ID 的人,这个 ID 存储在 Auth 模式中。一旦 someone 成为用户,就可以发放访问令牌(Access Token),用于访问 Supabase 的端点。这个令牌与用户绑定,所以你可以通过 RLS 策略 来限制对资源的访问。

🌐 A user in Supabase Auth is someone with a user ID, stored in the Auth schema. Once someone is a user, they can be issued an Access Token, which can be used to access Supabase endpoints. The token is tied to the user, so you can restrict access to resources via RLS policies.

永久和匿名用户 #

🌐 Permanent and anonymous users

Supabase 区分永久用户和匿名用户。

🌐 Supabase distinguishes between permanent and anonymous users.

  • 永久用户 与某个可识别个人身份的信息(PII)绑定,例如电子邮件地址、电话号码或第三方身份。他们可以使用这些身份在注销后重新登录自己的账户。
  • 匿名用户不绑定任何身份。他们有一个用户 ID 和个性化的访问令牌,但如果注销了,就无法以同一用户身份重新登录。

匿名用户的用途有:

🌐 Anonymous users are useful for:

  • 电子商务应用,在结账前创建购物车
  • 全功能演示,无需收集个人信息
  • 临时或一次性账户

查看 匿名登录指南 了解更多关于匿名用户的信息。

🌐 See the Anonymous Signins guide to learn more about anonymous users.

用户对象 #

🌐 The user object

用户对象存储了与你的应用中用户相关的所有信息。可以使用以下方法之一来获取用户对象:

🌐 The user object stores all the information related to a user in your application. The user object can be retrieved using one of these methods:

  1. supabase.auth.getUser()
  2. 使用 supabase.auth.admin.getUserById() 以管理员身份获取用户对象

用户可以用以下方法之一登录:

🌐 A user can sign in with one of the following methods:

  • 基于密码的方法(用邮箱或电话)
  • 无密码方式(通过邮箱或电话)
  • OAuth
  • SAML 单点登录

身份描述了用户可以用来登录的身份验证方法。一个用户可以有多个身份。支持的身份类型有:

🌐 An identity describes the authentication method that a user can use to sign in. A user can have multiple identities. These are the types of identities supported:

  • 电子邮件
  • 电话
  • OAuth
  • SAML

用户对象包含以下属性:

🌐 The user object contains the following attributes:

属性类型描述
idstring用户身份的唯一标识。
audstring观众声明。
角色stringPostgres 用于执行行级安全 (RLS) 检查的角色声明。
电子邮件string用户的电子邮件地址。
email_confirmed_atstring用户邮箱确认的时间戳。如果为 null,表示用户的邮箱尚未确认。
电话string用户的电话号码。
phone_confirmed_atstring用户手机确认的时间戳。如果为 null,表示用户的手机尚未确认。
confirmed_atstring用户的邮箱或电话确认的时间戳。如果为 null,表示用户没有确认的邮箱地址或电话号码。
last_sign_in_atstring用户上次登录的时间戳。
app_metadataobjectprovider 属性表示用户首次使用的注册提供商。providers 属性表示用户可用来登录的提供商列表。
用户元数据object默认为第一个提供者的身份数据,但如果指定,可以包含额外的自定义用户元数据。有关身份对象的更多信息,请参见 用户身份。不要依赖此字段中信息的顺序。不要在安全敏感的场景中使用它(例如 RLS 策略或授权逻辑),因为此值用户可以在没有任何检查的情况下编辑。
身份UserIdentity[]包含与用户关联的身份对象数组。
created_atstring用户创建的时间戳。
updated_atstring用户最后一次更新时间的时间戳。
is_anonymousboolean如果用户是匿名用户,则为真。

邀请用户 #

🌐 Inviting users

你可以通过发送邀请邮件邀请某人创建账户。被邀请的用户会收到一封包含链接的邮件,点击该链接后可以确认他们的电子邮件地址,并完成账户设置(例如,设置密码)。

🌐 You can invite someone to create an account by sending them an invitation email. The invited user receives an email containing a link that, when clicked, confirms their email address and lets them finish setting up their account (for example, by setting a password).

邀请用户是管理员操作,所以必须在受信任的服务器环境中使用你的密钥执行,或者在控制面板上操作。当你邀请一个还不是用户的邮箱时,会创建一个新的未确认用户。邀请已经是确认用户的邮箱会返回错误。

🌐 Inviting a user is an admin action, so it must be performed from a trusted server environment using your secret key, or from the Dashboard. When you invite an email that doesn't yet belong to a user, a new unconfirmed user is created. Inviting an email that already belongs to a confirmed user returns an error.

使用仪表板 #

🌐 Using the Dashboard

  1. 在仪表板中进入 身份验证 > 用户
  2. 点击 添加用户 并选择 发送邀请
  3. 输入用户的邮箱地址,然后点击 邀请用户

使用身份验证管理员 API #

🌐 Using the Auth Admin API

在服务器端环境中,从 SDK 的 Auth 管理 API 调用 inviteUserByEmail()。这是 Supabase Auth 的一部分(通过 supabase.auth.admin 使用你的项目密钥访问),与用于配置项目的 管理 API 不同。你可以选择附加自定义的 user_metadata 和邀请链接的重定向 URL。

🌐 Call inviteUserByEmail() from the SDK's Auth Admin API in a server-side environment. This is part of Supabase Auth (accessed via supabase.auth.admin with your project's secret key), and is distinct from the Management API used to configure your project. You can optionally attach custom user_metadata and a redirect URL for the invite link.

1
import { createClient } from '@supabase/supabase-js'
2
3
// Use your project's secret key (sb_secret_...), and only ever on a trusted server.
4
const supabase = createClient(process.env.SUPABASE_URL, process.env.SUPABASE_SECRET_KEY, {
5
auth: {
6
autoRefreshToken: false,
7
persistSession: false,
8
detectSessionInUrl: false,
9
},
10
})
11
12
const { data, error } = await supabase.auth.admin.inviteUserByEmail('someone@example.com', {
13
data: { name: 'Jane' }, // optional, stored in user_metadata
14
redirectTo: 'https://example.com/welcome', // optional, where the invite link sends the user
15
})

邀请邮件使用的是 邀请用户 邮件模板,你可以自行定制。要了解更多信息,请参考 邮件模板

🌐 The invitation email uses the Invite user email template, which you can customize. Refer to Email Templates to learn more.

资源 #

🌐 Resources