使用 Supabase

使用 Supabase 将 Postgres 数据库和用户身份验证添加到你的 React Native 应用。


Supabase 是一个后端即服务(BaaS)应用开发平台,提供托管的后端服务,如 Postgres 数据库、用户身份验证、文件存储、边缘函数、实时同步以及向量和 AI 工具包。它是 Google Firebase 的开源替代方案。

Supabase 会自动从你的数据库生成 REST API,并使用一种称为 行级安全 (RLS) 的概念来保护你的数据,使你可以直接从 React Native 应用与数据库交互,而无需通过服务器!

🌐 Supabase automatically generates a REST API from your database and employs a concept called row level security (RLS) to secure your data, making it possible to directly interact with your database from your React Native application without needing to go through a server!

Supabase 提供了一个名为 supabase-js 的 TypeScript 客户端库,用于与 REST API 交互。或者,Supabase 还提供了一个 GraphQL API,如果你愿意的话,你可以使用你喜欢的 GraphQL 客户端(例如 Apollo Client)来使用它。

🌐 Supabase provides a TypeScript client library called supabase-js to interact with the REST API. Alternatively, Supabase also exposes a GraphQL API allowing you to use your favorite GraphQL client (for example, Apollo Client) should you wish to.

先决条件

🌐 Prerequisites

前往 database.new 创建一个新的 Supabase 项目。

🌐 Head over to database.new to create a new Supabase project.

获取 API 密钥

🌐 Get the API Keys

从 API 设置中获取 项目 URL,并从 API 密钥中获取 可发布密钥

🌐 Get the Project URL from the API settings and Publishable key from the API Keys:

  1. 在仪表板中转到 API 设置 页面。
  2. 在此页面查找你的项目 URLservice_role 密钥。
  3. 然后转到 API密钥
  4. 在此页面的 API 密钥标签下找到你的项目 可发布密钥

使用 Supabase TypeScript SDK

🌐 Using the Supabase TypeScript SDK

使用 supabase-js 是利用 Supabase 整套技术栈全部功能的最便捷方式,因为它将所有不同的服务(数据库、认证、实时功能、存储、边缘函数)方便地整合在一起。

🌐 Using supabase-js is the most convenient way of leveraging the full power of the Supabase stack as it conveniently combines all the different services (database, auth, realtime, storage, edge functions) together.

安装并初始化 Supabase TypeScript SDK

🌐 Install and initialize the Supabase TypeScript SDK

1

在你创建好你的 Expo 项目 后,使用以下命令安装 @supabase/supabase-js 及所需的依赖:

🌐 After you have created your Expo project, install @supabase/supabase-js and the required dependencies using the following command:

Terminal
npx expo install @supabase/supabase-js expo-sqlite

2

创建一个辅助文件来初始化 Supabase 客户端(@supabase/supabase-js)。你需要之前复制过的 API URL 和 Publishable 密钥(见之前)。这些变量在你的 Expo 应用中是安全的,因为 Supabase 在数据库中启用了 行级安全

🌐 Create a helper file to initialize the Supabase client (@supabase/supabase-js). You need the API URL and the Publishable key copied earlier. These variables are safe to expose in your Expo app since Supabase has Row Level Security enabled in the Database.

utils/supabase.ts
import 'expo-sqlite/localStorage/install'; import { createClient } from '@supabase/supabase-js'; const supabaseUrl = YOUR_REACT_NATIVE_SUPABASE_URL; const supabasePublishableKey = YOUR_REACT_NATIVE_SUPABASE_PUBLISHABLE_KEY; export const supabase = createClient(supabaseUrl, supabasePublishableKey, { auth: { storage: localStorage, autoRefreshToken: true, persistSession: true, detectSessionInUrl: false, }, });

现在你可以在整个应用中使用 import { supabase } from '/utils/supabase' 来充分利用 Supabase 的全部功能!

🌐 Now you can import { supabase } from '/utils/supabase' throughout your application to leverage the full power of Supabase!

下一步

🌐 Next steps

构建用户管理应用

在本快速入门指南中学习如何结合使用 Supabase 身份验证和数据库。

使用 Apple 登录

Supabase Auth 支持在网页和 iOS、macOS、watchOS 或 tvOS 的原生应用中使用“使用 Apple 登录”。

使用 Google 登录

Supabase Auth 支持在网页、本地 Android 应用以及 Chrome 扩展中使用谷歌登录。

OAuth 和 Magic Link 的深层链接

在从原生移动应用执行 OAuth 或发送魔术链接邮件时,了解如何为 Android 和 iOS 应用配置深度链接。

使用 WatermelonDB 实现离线优先的 React Native 应用

了解如何使用 WatermelonDB 将数据本地存储并与 Postgres 同步。

使用 Supabase Storage 响应原生文件上传

学习如何在 React Native 应用中实现身份验证和文件上传。