This documentation is available as Markdown for AI agents and LLMs. See the full Markdown index or append .md to any documentation URL.

使用文员

学习如何在你的 Expo 和 React Native 项目中添加 Clerk 身份验证和用户管理。

Android
iOS
Web

Clerk 是一个认证和用户管理平台,提供注册、登录、多因素认证、社交登录、组织管理以及托管的用户数据库。@clerk/expo SDK 提供了 React 钩子、控制组件、托管认证,以及可以在 Android 上用 Jetpack Compose、在 iOS 上用 SwiftUI 渲染的预构建原生 UI 组件。

本指南向你展示如何安装 @clerk/expo,将你的应用封装在 <ClerkProvider> 中,以及选择最适合你项目的集成方式。它针对 @clerk/expo 4.x,支持 Expo SDK 54 及更高版本。

🌐 This guide shows you how to install @clerk/expo, wrap your app in <ClerkProvider>, and choose the integration approach that fits your project. It targets @clerk/expo 4.x, which supports Expo SDK 54 and later.

选择你的整合方法

🌐 Choose your integration approach

@clerk/expo 支持三种方法。选择一个符合你需求的即可。之后可以更改,无需重写你的应用。

方式你构建的内容在 Expo Go 中运行适合场景
托管认证一个按钮,在浏览器认证会话中打开 Clerk 的账户门户配置最快,仪表板中启用了所有方法
原生 UI 组件从 @clerk/expo/native 中直接使用 <AuthView />、<UserButton /> 和 <UserProfileView />完整的原生登录和账户管理界面
自定义流程你自己的 React Native 界面,调用如 useSignUp() 和 useSignIn() 的钩子最大的 UI 控制自由

先决条件

🌐 Prerequisites

先决条件

4 要求

1.

创建一个文员账户和应用

在 Clerk 控制台 注册并创建一个应用。

2.

启用本地 API

在 Clerk 控制台中打开 本地应用 页面,并确保 Native API 已开启。任何使用 @clerk/expo 的 Expo 集成都需要此设置。

3.

使用 Expo SDK 53 或更高版本

@clerk/expo Core 3 有一个对 expo: >=53 <56 的 peer 依赖。

4.

使用开发版以获取本地功能

本地 UI 组件和本地登录钩子需要开发版。托管认证和自定义流程在 Expo Go 中也能使用。

安装并配置 Clerk

🌐 Install and configure Clerk

1

安装 @clerk/expo 和 expo-secure-store

🌐 Install @clerk/expo and expo-secure-store

使用 npx expo install 以使版本与你的 Expo SDK 匹配:

🌐 Use npx expo install so versions match your Expo SDK:

Terminal
- npx expo install @clerk/expo expo-secure-store
- yarn expo install @clerk/expo expo-secure-store
- pnpm expo install @clerk/expo expo-secure-store
- bun expo install @clerk/expo expo-secure-store

expo-secure-store 是一个同级依赖。Clerk 通过 @clerk/expo/token-cache 使用它来使用 iOS 密钥串和 Android 密钥库加密会话令牌。

对于托管身份验证,还需要安装 Clerk 用来打开浏览器身份验证会话的包:

🌐 For hosted authentication, also install the packages Clerk uses to open the browser authentication session:

Terminal
- npx expo install expo-auth-session expo-crypto expo-web-browser
- yarn expo install expo-auth-session expo-crypto expo-web-browser
- pnpm expo install expo-auth-session expo-crypto expo-web-browser
- bun expo install expo-auth-session expo-crypto expo-web-browser

如果你打算在自定义流程中添加原生的 Google 登录按钮,安装 @clerk/expo-google-signin 和 expo-crypto:

🌐 If you plan to add native Sign in with Google buttons to a custom flow, install @clerk/expo-google-signin and expo-crypto:

Terminal
- npx expo install @clerk/expo-google-signin expo-crypto
- yarn expo install @clerk/expo-google-signin expo-crypto
- pnpm expo install @clerk/expo-google-signin expo-crypto
- bun expo install @clerk/expo-google-signin expo-crypto

对于原生的“使用 Apple 登录”按钮,安装 expo-apple-authentication 和 expo-crypto 两个即可:

🌐 For native Sign in with Apple buttons, install both expo-apple-authentication and expo-crypto:

Terminal
- npx expo install expo-apple-authentication expo-crypto
- yarn expo install expo-apple-authentication expo-crypto
- pnpm expo install expo-apple-authentication expo-crypto
- bun expo install expo-apple-authentication expo-crypto

如果你只使用 @clerk/expo/native 中的 <AuthView />,你不需要任何这些额外的包,因为该组件会在内部处理社交登录流程。

🌐 You do not need any of these extra packages if you only use <AuthView /> from @clerk/expo/native, since the component handles social sign-in flows internally.

2

验证配置插件

🌐 Verify the config plugins

在你的 app 配置 中将 @clerk/expo 和 expo-secure-store 添加到 plugins 数组。如果你的项目使用静态 app.json 并且你是用 npx expo install 安装的包,Expo 已经帮你添加好了:

🌐 Add @clerk/expo and expo-secure-store to the plugins array in your app config. If your project uses a static app.json and you installed the packages with npx expo install, Expo has already added them:

app.json
{ "expo": { "plugins": ["expo-secure-store", "@clerk/expo"] } }

@clerk/expo 插件会添加 Apple 登录权限(如果你的应用不使用,可以通过 appleSignIn: false 插件选项禁用),注册托管身份验证回调的 Android 意图过滤器,并应用底层 clerk-android SDK 所需的 Android 打包修复。如果你使用原生的 Google 登录按钮,也要同时添加 @clerk/expo-google-signin 插件。

🌐 The @clerk/expo plugin adds the Apple Sign In entitlement (disable it with the appleSignIn: false plugin option if your app doesn't use it), registers the Android intent filter for the hosted authentication callback, and applies the Android packaging fixes required by the underlying clerk-android SDK. If you use the native Sign in with Google buttons, also add the @clerk/expo-google-signin plugin alongside it.

托管认证的默认回调来自你应用配置中的 android.package 和 ios.bundleIdentifier 值。在创建生产构建之前,请在 Clerk 仪表板的 原生应用 页面上添加该应用,使用相同的 Android 包名和 iOS 打包标识,因为生产实例会根据注册的值验证回调。

🌐 Hosted authentication derives its default callback from the android.package and ios.bundleIdentifier values in your app config. Before you create a production build, add the app on the Native applications page in the Clerk Dashboard with the same Android package name and iOS bundle identifier, since production instances validate the callback against the registered values.

3

添加你的 Clerk 可发布密钥

🌐 Add your Clerk Publishable Key

从 Clerk 仪表板的 API keys 页面复制你的可发布密钥,然后将其添加到项目根目录下的 .env 文件中:

🌐 Copy your Publishable Key from the API keys page in the Clerk Dashboard, then add it to a .env file in the root of your project:

.env
EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY=pk_test_your-key-here

EXPO_PUBLIC_ 前缀是必需的,因为 Expo 会在构建时内联这些值,以便它们可以在你的 JavaScript 包中使用。Clerk 的可发布密钥是可以安全公开的。不要 将密钥放在 EXPO_PUBLIC_ 前缀下。

🌐 The EXPO_PUBLIC_ prefix is required because Expo inlines these values at build time so they are available in your JavaScript bundle. Clerk's Publishable Key is safe to expose. Do not put Secret Keys behind the EXPO_PUBLIC_ prefix.

4

将你的应用封装在 <ClerkProvider> 中

🌐 Wrap your app in <ClerkProvider>

在你的根布局文件(使用 Expo Router 的 src/app/_layout.tsx)中,将你的应用封装在 <ClerkProvider> 中并传入 Publishable Key。建议显式传入 tokenCache:

🌐 In your root layout file (src/app/_layout.tsx with Expo Router), wrap your app in <ClerkProvider> and pass the Publishable Key. Passing tokenCache explicitly is recommended:

src/app/_layout.tsx
import { ClerkProvider } from '@clerk/expo'; import { tokenCache } from '@clerk/expo/token-cache'; import { Slot } from 'expo-router'; const publishableKey = process.env.EXPO_PUBLIC_CLERK_PUBLISHABLE_KEY!; if (!publishableKey) { throw new Error('Add your Clerk Publishable Key to the .env file'); } export default function RootLayout() { return ( <ClerkProvider publishableKey={publishableKey} tokenCache={tokenCache}> <Slot /> </ClerkProvider> ); }

在 Core 3 中,对于 Expo 应用,<ClerkProvider> 上需要 publishableKey。在生产环境的 React Native 构建中,node_modules 内的环境变量不会被内联,因此必须明确传递该属性。

🌐 In Core 3, publishableKey is required on <ClerkProvider> for Expo apps. Environment variables inside node_modules are not inlined during production React Native builds, so the prop must be passed explicitly.

@clerk/expo/token-cache 的 tokenCache 使用 expo-secure-store 在应用重启时保持用户会话。显式传递它可以使依赖更加清晰,并允许你以后替换自定义缓存实现。

添加身份验证

🌐 Add authentication

下一步取决于你选择了哪种方法。下面的选项卡显示了每种方法的最少代码。

🌐 The next step depends on which approach you chose. The tabs below show the minimum code for each.

托管身份验证会在你的应用上通过浏览器身份验证会话打开 Clerk 的 账户门户。用户可以完成你 Clerk 应用启用的任何登录或注册方式,SDK 会在你的应用中激活生成的会话。从 useHostedAuth() 钩子调用 startHostedAuth():

🌐 Hosted authentication opens Clerk's Account Portal in a browser authentication session over your app. Users can complete any sign-in or sign-up method enabled for your Clerk application, and the SDK activates the resulting session in your app. Call startHostedAuth() from the useHostedAuth() hook:

src/app/index.tsx
import { useAuth } from '@clerk/expo'; import { useHostedAuth } from '@clerk/expo/hosted-auth'; import { ActivityIndicator, Button, Text, View } from 'react-native'; export default function MainScreen() { const { isLoaded, isSignedIn } = useAuth(); const { startHostedAuth } = useHostedAuth(); const handleSignUp = async () => { try { await startHostedAuth({ mode: 'sign-up' }); } catch (error) { // Handle the error in your app } }; if (!isLoaded) { return <ActivityIndicator size="large" />; } return ( <View> {isSignedIn ? ( <Text>You're signed in</Text> ) : ( <Button title="注册" onPress={handleSignUp} /> )} </View> ); }

身份验证完成后,SDK 会关闭浏览器会话,激活新的会话,并用已登录的状态更新 useAuth()。浏览器不会保留单独的活动会话。

🌐 After authentication completes, the SDK closes the browser session, activates the new session, and updates useAuth() with the signed-in state. The browser doesn't retain a separate active session.

startHostedAuth() 默认会打开登录页面并接受 mode: 'sign-in' | 'sign-up'。当用户在未完成操作时关闭浏览器,它会以 null createdSessionId 方式结束;如果认证失败,则会抛出错误。

这种方法在 Expo Go 中有效,因为 Expo 提供了一个开发回调。在开发或生产构建中,回调是根据你的 iOS bundle identifier 或 Android 包名生成的,所以在更改任一标识符后,保持 @clerk/expo 配置插件在你的应用配置中,并重新构建原生项目。

🌐 This approach works in Expo Go, where Expo supplies a development callback. In a development or production build, the callback is derived from your iOS bundle identifier or Android package name, so keep the @clerk/expo config plugin in your app config and rebuild the native project after changing either identifier.

账户门户在浏览器中运行,所以社交登录使用每个提供商的网页OAuth流程,而不是本地流程。有关生产凭证要求和故障排除,请参阅托管认证指南。

🌐 Account Portal runs in a browser, so social sign-in uses each provider's web OAuth flow rather than the native one. See the hosted authentication guide for production credential requirements and troubleshooting.

读取已登录的用户

🌐 Read the signed-in user

在你的应用中的任何地方,使用 useUser() 和 useAuth() 来读取用户数据,另外使用 <Show> 和 useClerk() 来保护内容并登出:

🌐 Anywhere in your app, use useUser() and useAuth() to read user data, plus <Show> and useClerk() to protect content and sign out:

import { Show, useClerk, useUser } from '@clerk/expo'; import { Link } from 'expo-router'; import { Pressable, Text, View } from 'react-native'; export default function HomeScreen() { const { user } = useUser(); const { signOut } = useClerk(); return ( <View> <Show when="signed-in"> <Text>Hello, {user?.firstName ?? 'friend'}</Text> <Pressable onPress={() => signOut()}> <Text>Sign out</Text> </Pressable> </Show> <Show when="signed-out"> <Link href="/(auth)/sign-in"> <Text>Sign in</Text> </Link> </Show> </View> ); }

<Show> 替代了早期版本 SDK 中的传统 <SignedIn>、<SignedOut> 和 <Protect> 组件。它还接受 when={{ role: '...' }}、when={{ permission: '...' }} 及其他授权谓词。

运行应用

🌐 Run the app

对于托管认证和自定义流程的方法,运行以下命令并在 Expo Go 中打开项目:

🌐 For the hosted authentication and custom flow approaches, run the following command and open the project in Expo Go:

Terminal
- npx expo start
- yarn expo start
- pnpm expo start
- bun expo start

下一步

🌐 Next steps

Clerk Expo 快速入门

为每种三种集成方法提供逐步设置说明,并附有 GitHub 上的配套仓库。

托管认证

使用 Clerk 的账户门户从你的 Expo 应用中登录和注册用户,支持回调、取消处理和生产环境设置。

本地组件引用

AuthView、UserButton 和 UserProfileView 的 API 参考,包括配置、主题和平台要求。

使用 Google 登录

通过 Clerk 控制台和 Google Cloud 控制台,为 Android 和 iOS 设置原生的 Google 登录。

使用 Apple 登录

设置原生“使用 Apple 登录”以符合 App Store 指南 4.8。

保护内容并读取用户数据

在你的 Expo 应用中使用 Clerk 的 hooks 和 Show 组件来保护路由并访问用户数据。

将 Expo 应用部署到生产环境并使用 Clerk

配置生产凭证,允许列表移动单点登录重定向,并使用 EAS 构建发布。