用户
在 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.
匿名用户不使用匿名角色
像固定用户一样,匿名用户也使用 authenticated 角色来访问数据库。
🌐 Like permanent users, anonymous users use the authenticated role for database access.
anon 角色是给那些完全没有登录、没有绑定任何用户 ID 的人。我们称这些人为未认证用户或公众用户。
🌐 The anon role is for those who aren't signed in at all and are not tied to any user ID. We refer to these as unauthenticated or public 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:
supabase.auth.getUser()- 使用
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
拥有电子邮件或电话号码身份的用户可以使用密码或无密码方式(例如使用一次性密码(OTP)或魔法链接)登录。默认情况下,未验证邮箱或电话号码的用户将无法登录。
🌐 A user with an email or phone identity will be able to sign in with either a password or passwordless method (e.g. use a one-time password (OTP) or magic link). By default, a user with an unverified email or phone number will not be able to sign in.
用户对象包含以下属性:
🌐 The user object contains the following attributes:
| 属性 | 类型 | 描述 |
|---|---|---|
| id | string | 用户身份的唯一标识。 |
| aud | string | 观众声明。 |
| 角色 | string | Postgres 用于执行行级安全 (RLS) 检查的角色声明。 |
| 电子邮件 | string | 用户的电子邮件地址。 |
| email_confirmed_at | string | 用户邮箱确认的时间戳。如果为 null,表示用户的邮箱尚未确认。 |
| 电话 | string | 用户的电话号码。 |
| phone_confirmed_at | string | 用户手机确认的时间戳。如果为 null,表示用户的手机尚未确认。 |
| confirmed_at | string | 用户的邮箱或电话确认的时间戳。如果为 null,表示用户没有确认的邮箱地址或电话号码。 |
| last_sign_in_at | string | 用户上次登录的时间戳。 |
| app_metadata | object | provider 属性表示用户首次使用的注册提供商。providers 属性表示用户可用来登录的提供商列表。 |
| 用户元数据 | object | 默认为第一个提供者的身份数据,但如果指定,可以包含额外的自定义用户元数据。有关身份对象的更多信息,请参见 用户身份。不要依赖此字段中信息的顺序。不要在安全敏感的场景中使用它(例如 RLS 策略或授权逻辑),因为此值用户可以在没有任何检查的情况下编辑。 |
| 身份 | UserIdentity[] | 包含与用户关联的身份对象数组。 |
| created_at | string | 用户创建的时间戳。 |
| updated_at | string | 用户最后一次更新时间的时间戳。 |
| is_anonymous | boolean | 如果用户是匿名用户,则为真。 |
邀请用户 #
🌐 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
- 在仪表板中进入 身份验证 > 用户。
- 点击 添加用户 并选择 发送邀请。
- 输入用户的邮箱地址,然后点击 邀请用户。
使用身份验证管理员 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.
1import { createClient } from '@supabase/supabase-js'23// Use your project's secret key (sb_secret_...), and only ever on a trusted server.4const supabase = createClient(process.env.SUPABASE_URL, process.env.SUPABASE_SECRET_KEY, {5 auth: {6 autoRefreshToken: false,7 persistSession: false,8 detectSessionInUrl: false,9 },10})1112const { data, error } = await supabase.auth.admin.inviteUserByEmail('someone@example.com', {13 data: { name: 'Jane' }, // optional, stored in user_metadata14 redirectTo: 'https://example.com/welcome', // optional, where the invite link sends the user15})秘密密钥(sb_secret_...,取代了旧的 service_role 密钥)可以绕过行级安全,必须只在安全的服务器环境中使用。切勿在浏览器或任何公开可访问的客户端中暴露它。
🌐 The secret key (sb_secret_..., which replaces the legacy service_role key) bypasses Row Level Security and must only be used in a secure server environment. Never expose it in a browser or any publicly accessible client.
redirectTo URL 必须在你项目的允许的重定向 URL配置中。如果没有,它的 redirectTo 值会被忽略,邀请链接会重定向到你的站点 URL(不会报错)。
🌐 The redirectTo URL must be in your project's allowed redirect URLs configuration. If it isn't, the redirectTo value is ignored and the invite link redirects to your Site URL instead (no error is raised).
邀请邮件使用的是 邀请用户 邮件模板,你可以自行定制。要了解更多信息,请参考 邮件模板。
🌐 The invitation email uses the Invite user email template, which you can customize. Refer to Email Templates to learn more.
邀请链接会在 Email OTP Expiration 中配置的时间后过期,默认是 1 小时。这和 email OTPs、魔法链接以及其他电子邮件确认链接使用的时间相同。如果邀请在被接受之前就过期了,请给用户发送新的邀请。
🌐 Invitation links expire after the duration configured in Email OTP Expiration, which defaults to 1 hour. This is the same value used for email OTPs, magic links, and other email confirmation links. If an invitation expires before it's accepted, send the user a new invite.
资源 #
🌐 Resources