更好的 Auth 博览会插件
此插件将更好的 Auth 与 Expo 集成, 允许您轻松添加 认证您的 Expo( React Industrial) 应用程序 . 它支持博览会本土和网络应用.
安装
要启动, 请安装必要的软件包 :
# Using npm
npm install better-auth @better-auth/expo
# Using yarn
yarn add better-auth @better-auth/expo
# Using pnpm
pnpm add better-auth @better-auth/expo
# Using bun
bun add better-auth @better-auth/expo
您还需要安装 expo-secure-store 用于安全的会话和饼干
在您的 Expo 应用程序中存储 :
npm install expo-secure-store
# or
yarn add expo-secure-store
# or
pnpm add expo-secure-store
# or
bun add expo-secure-store
基本用途
配置更好的 Auth 后端
确保设置更好的 Auth 后端 . 你可以跟着主 安装指南。 。 。 .
然后,将博览会插件添加到您的更好的 Auth 服务器配置中(例如,在
auth.ts 或者说 lib/auth.ts 文件名 :
// lib/auth.ts
import { betterAuth } from 'better-auth';
import { expo } from '@better-auth/expo'; // Import the server plugin
export const auth = betterAuth({
// ...your other Better Auth options
baseURL: 'http://localhost:8081', // The base URL of your application server where the routes are mounted.
plugins: [expo()], // Add the Expo server plugin
emailAndPassword: {
enabled: true,
},
// Add other configurations like trustedOrigins
trustedOrigins: ['myapp://'], // Replace "myapp" with your app's scheme
});
在 Expo 中初始化更好的 Auth 客户端
在您的 Expo app中,初始化客户端( 如: lib/auth-client.ts:
// lib/auth-client.ts
import { createAuthClient } from 'better-auth/react';
import { expoClient } from '@better-auth/expo/client'; // Import the client plugin
import * as SecureStore from 'expo-secure-store';
export const authClient = createAuthClient({
baseURL: 'http://localhost:8081', // Your Better Auth backend URL
plugins: [
expoClient({
scheme: 'myapp', // Your app's scheme (defined in app.json)
storagePrefix: 'myapp', // A prefix for storage keys
storage: SecureStore, // Pass SecureStore for token storage
}),
],
});
// You can also export specific methods if you prefer:
// export const { signIn, signUp, useSession } = authClient;
请确定您的应用方案( 例如“ mayapp ”) 在您的 app.json。 。 。 .
文档
关于更详细的资料和高级配置,请参考 文档:
许可证
麻省理工学院