Skip to content
Auth

Auth0

Use Auth0 with your Supabase project

Auth0 可以和 Supabase Auth 一起作为第三方身份验证提供商使用,也可以单独与你的 Supabase 项目搭配使用。

🌐 Auth0 can be used as a third-party authentication provider alongside Supabase Auth, or standalone, with your Supabase project.

入门 #

🌐 Getting started

  1. 首先,你需要添加一个集成,将你的 Supabase 项目与 Auth0 租户连接起来。你需要你的租户 ID(在某些情况下还需要区域 ID)。
  2. 在你项目的身份验证设置中添加一个新的第三方认证集成。
  3. 通过使用 Auth0 Action,将 role: 'authenticated' 自定义声明分配给所有 JWT。
  4. 终于在你的应用中设置了 Supabase 客户端。

设置 Supabase 客户端库 #

🌐 Setup the Supabase client library

1
import { createClient } from '@supabase/supabase-js'
2
import { createAuth0Client } from '@auth0/auth0-spa-js'
3
4
const auth0 = await createAuth0Client({
5
domain: '<AUTH0_DOMAIN>',
6
clientId: '<AUTH0_CLIENT_ID>',
7
authorizationParams: {
8
redirect_uri: '<MY_CALLBACK_URL>',
9
},
10
})
11
12
const supabase = createClient(
13
'https://<supabase-project>.supabase.co',
14
'SUPABASE_PUBLISHABLE_KEY',
15
{
16
accessToken: async () => {
17
// Use the ID token which reliably includes custom claims.
18
const idToken = (await auth0.getIdTokenClaims())?.__raw
19
if (!idToken) throw new Error('Missing ID token')
20
return idToken
21
},
22
}
23
)

在你的项目中添加一个新的第三方认证集成 #

🌐 Add a new Third-Party Auth integration to your project

在仪表板中,进入你项目的 身份验证设置,找到第三方认证部分,添加一个新的集成。

🌐 In the dashboard navigate to your project's Authentication settings and find the Third-Party Auth section to add a new integration.

在命令行接口中,将以下配置添加到你的 supabase/config.toml 文件:

🌐 In the CLI add the following config to your supabase/config.toml file:

1
[auth.third_party.auth0]
2
enabled = true
3
tenant = "<id>"
4
tenant_region = "<region>" # if your tenant has a region

使用 Auth0 动作来分配已认证的角色 #

🌐 Use an Auth0 Action to assign the authenticated role

你的 Supabase 项目会检查所有发送给它的 JWT 中的 role 声明,以便在使用 Data API、Storage 或 Realtime 授权时分配正确的 Postgres 角色。

🌐 Your Supabase project inspects the role claim present in all JWTs sent to it, to assign the correct Postgres role when using the Data API, Storage or Realtime authorization.

默认情况下,Auth0 的 JWT(包括访问令牌和 ID 令牌)中不包含 role 声明。如果你把这样的 JWT 发送到你的 Supabase 项目,当执行 Postgres 查询时,会分配 anon 角色。你大部分的应用逻辑都可以通过 authenticated 角色访问。

🌐 By default, Auth0 JWTs (both access token and ID token) do not contain a role claim in them. If you were to send such a JWT to your Supabase project, the anon role would be assigned when executing the Postgres query. Most of your app's logic will be accessible by the authenticated role.

配置 onExecutePostLogin Auth0 操作 将自定义声明添加到 ID 令牌 中:

🌐 Configure the onExecutePostLogin Auth0 Action to add the custom claim to ID tokens:

1
exports.onExecutePostLogin = async (event, api) => {
2
api.idToken.setCustomClaim('role', 'authenticated')
3
}

限制 #

🌐 Limitations

目前,不支持使用以下签名算法的 Auth0 租户:

🌐 At this time, Auth0 tenants with the following signing algorithms are not supported:

  • HS256(使用 SHA-256 的 HMAC)——也叫对称 JWT
  • PS256(带 SHA-256 的 RSA-PSS)