亚马逊 Cognito(Amplify)
Use Amazon Cognito via Amplify or standalone with your Supabase project
Amazon Cognito 用户池(通过 AWS Amplify 或单独使用)可以作为第三方认证提供者与 Supabase Auth 一起使用,也可以单独用于你的 Supabase 项目。
🌐 Amazon Cognito User Pools (via AWS Amplify or on its own) can be used as a third-party authentication provider alongside Supabase Auth, or standalone, with your Supabase project.
入门 #
🌐 Getting started
- 首先,你需要添加一个集成,将你的 Supabase 项目与 Amazon Cognito 用户池连接起来。你需要这个用户池的 ID 和区域信息。
- 在你项目的身份验证设置中添加新的第三方认证集成,或者在 CLI 中进行配置。
- 通过使用预令牌生成触发器,将
role: 'authenticated'自定义声明分配给所有 JWT。 - 终于在你的应用中设置了 Supabase 客户端。
设置 Supabase 客户端库 #
🌐 Setup the Supabase client library
1import { fetchAuthSession, Hub } from 'aws-amplify/auth'23const supabase = createClient(4 'https://<supabase-project>.supabase.co',5 'SUPABASE_PUBLISHABLE_KEY',6 {7 accessToken: async () => {8 const tokens = await fetchAuthSession()910 // Alternatively you can use tokens?.idToken instead.11 return tokens?.accessToken12 },13 }14)1516// if you're using Realtime you also need to set up a listener for Cognito auth changes17Hub.listen('auth', () => {18 fetchAuthSession().then((tokens) => supabase.realtime.setAuth(tokens?.accessToken))19})在你的项目中添加一个新的第三方认证集成 #
🌐 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.aws_cognito]2enabled = true3user_pool_id = "<id>"4user_pool_region = "<region>"使用预先生成令牌的触发器来分配已认证角色 #
🌐 Use a pre-token generation trigger 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.
默认情况下,Amazon Cognito 的 JWT(包括 ID token 和 access token)里不包含 role 声明。如果你把这样的 JWT 发到你的 Supabase 项目,在执行 Postgres 查询时会被分配 anon 角色。你应用的大部分逻辑都可以通过 authenticated 角色访问。
🌐 By default, Amazon Cognito JWTs (both ID token and access tokens) 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.
推荐的方法是配置一个 预令牌生成触发器,可以选择 V1_0(仅 ID 令牌)或 V2_0(访问令牌和 ID 令牌)。为此,你需要创建一个新的 Lambda 函数(可以用任何语言和运行时),并将其分配到 Amazon Cognito 用户池的 Lambda 触发器配置。例如,Lambda 函数应该类似这样:
🌐 A recommended approach to do this is to configure a Pre-Token Generation Trigger either V1_0 (ID token only) or V2_0 (both access and ID token). To do this you will need to create a new Lambda function (in any language and runtime) and assign it to the Amazon Cognito User Pool's Lambda Triggers configuration. For example, the Lambda function should look similar to this:
1export const handler = async (event) => {2 event.response = {3 claimsOverrideDetails: {4 claimsToAddOrOverride: {5 role: 'authenticated',6 },7 },8 }910 return event11}