This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.
使用 Supabase
使用 Supabase 在你的 React Native 应用中添加 Postgres 数据库和用户认证。
Supabase 是一个基于 Postgres 构建的后端即服务 (BaaS) 应用开发平台。它可以从你的数据库生成 REST API,并使用行级安全来保护数据,所以你的 React Native 应用可以直接查询这个 API,中间不需要服务器。
EAS CLI 集成可以自动补齐标准设置:授权你的 Supabase 账户、创建或关联项目、安装 SDK,并填写环境变量。你也可以手动设置并照常使用本指南的其他部分。
🌐 The EAS CLI integration automates the standard setup: authorizing your Supabase account, creating or linking a project, installing the SDK, and writing your environment variables. You can also set it up manually and use the rest of this guide unchanged.
4 要求
4 要求
1.
注册一个 Expo 账户。
2.
npm install -g eas-cli 全局安装 EAS CLI。3.
创建一个 Expo 项目并用 eas init 将其连接到 EAS。
4.
注册一个 Supabase 账户。
你将学到什么
🌐 What you'll learn
- 在你的 React Native 应用中安装和配置 Supabase
- [使用你在步骤2中创建的客户端]添加身份验证
- 为本地开发、生产环境和预览环境 设置环境
- 管理集成 和 排查常见问题
安装并配置 Supabase
🌐 Install and configure Supabase
1
运行 connect 命令
🌐 Run the connect command
在你的项目目录中运行以下命令:
🌐 Run the following command in your project directory:
没有关联项目时,该命令会创建一个新的 Supabase 项目。如果想使用已有的 Supabase 项目,请改为关联它:
🌐 With no project linked, the command creates a new Supabase project. To use a Supabase project you already have, link it instead:
这个命令:
🌐 This command:
- 会打开你的浏览器来授权 Supabase,然后在你批准后继续。
- 如果你的账户有多个,它会询问要使用哪个 Supabase 组织。
- 请选择来自美洲、欧洲/中东/非洲和亚太地区的区域,然后创建项目并等待其准备就绪。区域会决定你的数据驻留位置,项目创建后无法更改。
- 安装
@supabase/supabase-js和expo-sqlite,它们会存储认证会话,并将expo-sqlite配置插件添加到你的 应用配置 中。如果你使用 动态应用配置,该命令会打印出插件条目供你添加。 - 将
EXPO_PUBLIC_SUPABASE_URL和EXPO_PUBLIC_SUPABASE_PUBLISHABLE_KEY写入 .env.local,并写入你在生产、预览和开发环境中的 EAS 环境变量。
这两个值都是公开的,所以你的应用可以使用它们。任何有这些值的人都可以查询你的数据库,所以行级安全就是保护你数据隐私的方式。启用它,并在你的应用读取或写入的每个表上添加策略。
🌐 Both values are meant to be public, so your app can ship them. Anyone who has them can query your database, so row level security is what keeps your data private. Enable it and add policies on every table your app reads or writes.
警告 切勿在你的应用中放置数据库密码或密钥。密钥会绕过行级安全,并授予对你数据的完全访问权限。
重新运行 connect 是安全的:它会重用你现有的连接和 Supabase 项目,并在覆盖环境变量前进行提示。
🌐 Re-running connect is safe: it reuses your existing connection and Supabase project, and prompts before overwriting environment variables.
在 CI 中或非交互方式运行
EAS Build 和 EAS Update 会从它们运行的环境中读取变量,所以只有在 CI 创建或链接项目时才在 CI 中运行 connect:
🌐 EAS Build and EAS Update read the variables from the environment they run in, so run connect in CI only when CI creates or links the project:
--region在命令在不提示的情况下创建项目时是必需的。它可以使用americas、emea、apac或特定代码,例如us-east-1。--overwrite会直接替换已有的环境变量,不会提示你。--organization选择一个 Supabase 组织。--json意味着--non-interactive。
授权 Supabase 账户需要浏览器,所以至少先交互式运行一次 connect。
🌐 Authorizing a Supabase account needs a browser, so run connect interactively at least once first.
2
创建 Supabase 客户端
🌐 Create the Supabase client
创建一个辅助文件,从环境变量 connect 初始化客户端。这里的路径遵循默认的 Expo 模板,其中 @/ 对应 src。查看 tsconfig.json 中的 paths 字段,并把文件放在你的别名解析的路径下,如果项目没有别名,就用相对导入。
🌐 Create a helper file that initializes the client from the environment variables connect writes. Paths here follow the default Expo template, where @/ maps to src. Check the paths field in tsconfig.json and put the file where your alias resolves, or use a relative import if your project has no alias.
expo-sqlite/localStorage/install 提供了 Supabase 用来在设备上持久化会话的 localStorage,这样用户在多次打开应用时都能保持登录状态。 detectSessionInUrl 是 false,因为 Android 和 iOS 没有 URL 可以用来读取会话。 Supabase 官方的快速开始指南也会导入 react-native-url-polyfill/auto,但 Expo 项目不需要,因为 Expo 已经安装了一个全局的 URL。
3
创建一个表格
🌐 Create a table
运行 eas integrations:supabase:dashboard 来打开你的关联项目,或者从 Supabase 仪表板打开它。选择 SQL 编辑器,然后运行:
🌐 Run eas integrations:supabase:dashboard to open your linked project, or open it from the Supabase dashboard. Select SQL Editor, then run:
请求在未登录时使用 anon 角色,登录后使用 authenticated 角色,策略决定这些角色可以读取哪些行。如果没有策略,select 会返回一个空数组且不会报错。要让应用可以写入,需要添加一个插入策略。
🌐 Requests use the anon role when signed out and authenticated after sign-in, and the policy decides which rows those roles can read. Without a policy, a select returns an empty array and no error. To let the app write, add an insert policy.
这个授权更像是一种保障,而不是必须。已经托管的项目会在 public 的新表上向两个角色授予 select、insert、update 和 delete。然而,Supabase 正在让这些授权变成自选,如果项目被撤销这些权限,就会报 permission denied for table todos 错误。给一个表已经拥有的权限实际上不会改变任何东西。
🌐 The grant is a safeguard rather than a requirement. Hosted projects already grant select, insert, update, and delete on new tables in public to both roles. However, Supabase is making those grants opt-in, and a project with them revoked fails with permission denied for table todos. Granting a privilege the table already has changes nothing.
4
验证配置
🌐 Verify the configuration
用对表的查询替换你第一个屏幕的内容:
🌐 Replace the contents of your first screen with a query against the table:
启动你的应用:
🌐 Start your app:
如果屏幕显示 Hello from Supabase,说明你的客户端可以正常工作。屏幕为空表示没有策略允许读取。出现错误信息则表示其他问题,请参见 故障排除。
🌐 If the screen shows Hello from Supabase, your client works. An empty screen means no policy allows the read. An error message means something else, so see Troubleshooting.
你可以在 Expo Go 中测试本指南中的所有内容。一旦你向 expo-sqlite 插件传递选项或添加其他原生库,你就需要一个开发构建 。
🌐 You can test everything in this guide in Expo Go. You need a development build once you pass options to the expo-sqlite plugin or add other native libraries.
有关这些步骤背后的概念,请参阅 Supabase 数据库概览:
🌐 For the concepts behind these steps, see the Supabase database overview:
表格、行级安全策略和实时更新。
添加身份验证
🌐 Add authentication
来自 Step 2 的客户端已经存储了会话,所以使用邮箱和密码登录不需要额外配置。新的 Supabase 项目默认会确认邮箱地址。你的第一个 signUp 会返回 data.user,并且 data.session 会设置为 null,直到地址被确认,或者你在项目的邮箱提供商设置中关闭 确认邮箱。
🌐 The client from Step 2 already stores sessions, so email and password sign-in needs no extra configuration. New Supabase projects confirm email addresses by default. Your first signUp returns data.user with data.session set to null until the address is confirmed or you turn off Confirm email in your project's email provider settings.
autoRefreshToken 会在 Android 和 iOS 上持续运行其刷新循环,所以 Supabase 的 startAutoRefresh 参考 建议将循环绑定到应用状态。在你的客户端文件中添加这个内容:
OAuth 提供商和魔术链接需要一个深度链接返回到你的应用,详见 Supabase 的移动深度链接指南。
🌐 OAuth providers and magic links need a deep link back into your app, covered in Supabase's mobile deep linking guide.
设置环境
🌐 Set up environments
connect 将这些值存储为 EAS 环境变量,所以每次构建或更新都会从运行的环境中读取它们。
一个 Supabase 项目包含一个环境的数据,所以不同的环境就意味着要有不同的项目。在创建第二个项目之前,先查看你的 Supabase 计划限制。
🌐 A Supabase project holds one environment's data, so separate environments mean separate projects. Check your Supabase plan limits before creating a second project.
开发 可以在本地使用 Supabase CLI 运行。先启动 Docker,然后运行:
用 supabase status 打印的 API URL 和可发布密钥替换 .env.local 中的托管值。你在 shell 中导出的变量优先于 .env.local,所以编辑文件就行,不用导出。因为本地数据库是空的,所以也要对它运行你的表 SQL。重新运行 connect 会把托管值写回去。
🌐 Replace the hosted values in .env.local with the API URL and publishable key that supabase status prints. Variables you export in your shell take precedence over .env.local, so edit the file instead of exporting. The local database starts empty, so run your table SQL against it too. Re-running connect writes the hosted values back.
信息 本地 URL 指向你的电脑。物理设备或 Android 模拟器无法访问它,所以在那儿使用你电脑的局域网地址,而不是
127.0.0.1。
将开发环境指向本地堆栈
connect 将每个值写成一个变量,涵盖生产、预览和开发环境。eas env:pull --environment development 会在替换 .env.local 前询问,然后从那个环境重写整个文件,所以你设置的本地值会被覆盖。
如果想让 Development 指向本地环境的 stack,可以将那个变量换成两个。eas env:set 重用了一个同名变量,它的环境与目标环境有重叠,所以如果只为 Development 设置,会导致 Production 和 Preview 也指向本地 URL:
🌐 To point Development at a local stack instead, replace that one variable with two. eas env:set reuses a variable of the same name whose environments overlap the target, so a Development-only set would move Production and Preview to the local URL too:
如果 eas.json 中的任何构建配置设置了 "environment": "development",就不要这么做。云构建会嵌入一个设备无法访问的 URL。
🌐 Don't do this if any build profile in eas.json sets "environment": "development". A cloud build then embeds a URL that no device can reach.
生产 使用 connect 设置的项目。
预览 默认没有自己的项目。要给预览或任何其他 EAS 环境分配自己的托管项目,请使用 --environment 重新运行 connect。这将创建第二个项目,并计入你计划的活跃项目限制:
新项目的 URL 和密钥只会指向指定的环境,而你的其他环境仍然指向第一个项目。如果目标环境中已经有 EXPO_PUBLIC_SUPABASE_* 值,命令会在替换前要求确认。和没有 --environment 的 connect 不同,这不会安装 SDK,也不会修改 .env.local。你不能将 --environment 和 --link、--reauth 或 --organization 结合使用。
🌐 The new project's URL and key go to the named environments only, and your other environments keep pointing at the first project. If the target environments already hold EXPO_PUBLIC_SUPABASE_* values, the command asks to confirm before replacing them. Unlike connect without --environment, this doesn't install the SDK or touch .env.local. You can't combine --environment with --link, --reauth, or --organization.
要让 EAS 环境指向你已有的 Supabase 项目,可以自己用 eas env:set 设置这两个变量,而不是使用 --environment。先用两个替换共享变量,如上所示,这样你的其他环境就能保持当前项目不变。
🌐 To point an EAS environment at a Supabase project you already have, set the two variables yourself with eas env:set rather than using --environment. Replace the shared variable with two first, as shown above, so your other environments keep their current project.
管理整合
🌐 Manage the integration
之后有两个命令来管理集成:
🌐 Two commands manage the integration afterward:
dashboard 会打开你已关联的 Supabase 项目。disconnect 只会移除 Expo 端的关联。你的 Supabase 项目、数据以及环境变量都不会受影响。
断开连接后,connect 不再看到已关联的项目,因此它会创建一个新项目。要让应用指回同一个项目,运行带有其参考ID的 connect --link 就可以了。
🌐 After you disconnect, connect no longer sees a linked project, so it creates a new one. To point the app back at the same project, run connect --link with its reference ID.
手动设置
🌐 Manual setup
connect 是标准 Supabase 设置的快捷方式。要手动操作的话:
-
在 database.new 创建一个项目。
-
安装 SDK:
Terminal -
将
expo-sqlite配置插件添加到你的 应用配置 中。 -
在 .env.local 中设置
EXPO_PUBLIC_SUPABASE_URL和EXPO_PUBLIC_SUPABASE_PUBLISHABLE_KEY,然后从 步骤 2 创建客户端文件。
故障排除
🌐 Troubleshooting
活跃项目已达上限
免费计划允许每个用户拥有两个活跃项目,一个组织会将其中每个拥有者和管理员的配额汇总。暂停的项目不计入配额。你可以用 --link 关联你已有的项目,在 Supabase 控制面板暂停或删除某个项目,升级组织,或者查看 Supabase 的 计费常见问题 了解配额如何共享。--environment 不能和 --link 合并,所以在那种情况下,你需要免费释放一个插槽,或者在目标环境上自己设置 EXPO_PUBLIC_SUPABASE_URL 和 EXPO_PUBLIC_SUPABASE_PUBLISHABLE_KEY。
🌐 The Free plan entitles each user to two active projects, and an organization pools the entitlement of every owner and administrator in it. Paused projects don't count. Link a project you already have with --link, pause or delete one in the Supabase dashboard, upgrade the organization, or see Supabase's billing FAQ for how the entitlement is shared. --environment can't be combined with --link, so on that path free a slot or set EXPO_PUBLIC_SUPABASE_URL and EXPO_PUBLIC_SUPABASE_PUBLISHABLE_KEY on the target environments yourself.
项目参考ID不起作用
该项目必须属于你连接的 Supabase 组织。有关可接受格式,请参见 查找你的项目参考 ID。
🌐 The project must belong to the Supabase organization you connected. See Finding your project reference ID for the accepted formats.
环境变量没有更新
在 connect 写入它们之后,重启一下应用,这样它就能使用新的值。如果应用还是读取旧值,停止开发服务器,然后再次运行 npx expo start。
🌐 After connect writes them, reload the app so it picks up the new values. If the app still reads the old value, stop the development server and run npx expo start again.
刚创建表时它不存在
Supabase 会缓存你的数据库模式,所以在你创建表之后立刻查询可能会因为 Could not find the table 'public.todos' in the schema cache 失败。再运行一次;缓存会自动刷新。
🌐 Supabase caches your database schema, so a query right after you create a table can fail with Could not find the table 'public.todos' in the schema cache. Re-run it; the cache refreshes on its own.
表的权限被拒绝
为两个角色添加你的查询所需的权限,例如 grant select on public.todos to anon, authenticated。单独的策略是不够的。不同于缺少策略会返回空数组的情况,这会报错 error.code 42501。
🌐 Add the grant your query needs for both roles, such as grant select on public.todos to anon, authenticated. A policy alone isn't enough. Unlike a missing policy, which returns an empty array, this fails with error.code 42501.
Supabase 授权停止工作了
如果你在 Supabase 中撤销了 Expo 的访问权限,运行 eas integrations:supabase:connect --reauth。这会清除已存储的连接和项目链接,重新打开浏览器,然后会询问你是要关联项目还是创建新项目,所以准备好你的参考 ID。你的 Supabase 项目不会受影响。--reauth 需要浏览器,因此在非交互模式下会失败。
🌐 If you revoked Expo's access in Supabase, run eas integrations:supabase:connect --reauth. This clears the stored connection and project link, reopens the browser, then asks whether to link a project or create one, so have your reference ID ready. Your Supabase projects aren't affected. --reauth needs a browser, so it fails in non-interactive mode.
额外项目已创建,但没有环境变量
如果 connect --environment 创建了一个项目,但之后未能写入环境变量,命令会打印项目 URL 和可发布密钥。使用 eas env:set 保存这些值。
🌐 If connect --environment creates a project and then fails to write the environment variables, the command prints the project URL and publishable key. Save those values with eas env:set.
警告 在这种情况下不要重新运行
connect --environment。它会创建另一个项目,这会计入你的计划限制。
进一步阅读
🌐 Further reading
在这个快速入门指南中把 Supabase 认证和数据库结合起来。
使用 Supabase Auth 在你的 Android 和 iOS 应用中添加“用 Apple 登录”。
使用 Supabase Auth 在你的 Android 和 iOS 应用中添加 Google 登录。
把你的数据存储在本地,并用 WatermelonDB 与 Postgres 同步。
在 React Native 应用里实现身份验证和文件上传。