diff --git a/app/auth.tsx b/app/auth.tsx index 258b992..4aa1130 100644 --- a/app/auth.tsx +++ b/app/auth.tsx @@ -4,6 +4,7 @@ import { Ionicons } from '@expo/vector-icons' import { ScrollView, Dimensions, TextInput, Platform } from 'react-native' import { useAuth } from '@/hooks/core/use-auth' import { KeyboardAvoidingView } from 'react-native-keyboard-controller' +import { setAuthToken } from '@/lib/auth' const BACKGROUND_VIDEOS = [ @@ -40,6 +41,16 @@ export default function Auth() { const result = await signIn.username({ username, password, + }, { + onSuccess: async (ctx) => { + const authToken = ctx.response.headers.get("set-auth-token"); + if (authToken) { + setAuthToken(authToken) + } + }, + onError: (ctx) => { + console.error(`[LOGIN] login error`, ctx) + } }) if (result.error) { @@ -80,7 +91,7 @@ export default function Auth() { }) console.log('result------------', result); - + if (result.error) { Toast.show({ title: result.error.message || '注册失败' }) } else { @@ -102,7 +113,7 @@ export default function Auth() { }, [mode, handleLogin, handleRegister]) return ( - + diff --git a/lib/auth.ts b/lib/auth.ts index d8ae5fd..bbc494a 100644 --- a/lib/auth.ts +++ b/lib/auth.ts @@ -1,13 +1,13 @@ import "reflect-metadata"; import { createAuthClient } from "better-auth/react"; import { usernameClient, phoneNumberClient, emailOTPClient, genericOAuthClient, adminClient, organizationClient, jwtClient } from 'better-auth/client/plugins' -import * as SecureStore from "expo-secure-store"; import { expoClient } from "@better-auth/expo/client"; import { createSkerClientPlugin } from '@repo/sdk' import { stripeClient } from './plugins/stripe-plugin'; import type { Subscription } from './plugins/stripe'; import type { ApiError } from './types'; import { fetchWithLogger } from './fetch-logger'; +import { storage } from './storage'; export interface CreditBalance { tokenUsage: number; @@ -62,12 +62,25 @@ export interface ISubscription { restore: (params?: SubscriptionRestoreParams) => Promise<{ data?: SubscriptionRestoreResponse; error?: ApiError }>; billingPortal: (params?: BillingPortalParams) => Promise<{ data?: BillingPortalResponse; error?: ApiError }>; } +export const getAuthToken = async () => + (await storage.getItem('bestaibest.better-auth.session_token')) || ''; + +export const setAuthToken = async (token: string)=>{ + await storage.setItem(`bestaibest.better-auth.session_token`, token); +} export const authClient = createAuthClient({ baseURL: "https://api-test.mixvideo.bowong.cc", trustedOrigins: ["duooomi://", "https://api-test.mixvideo.bowong.cc"], - storage: SecureStore, + storage, scheme: "duooomi", fetchOptions: { + auth: { + type: 'Bearer', + token: async () => { + const Authorization = await getAuthToken() + return Authorization; + } + }, customFetchImpl: fetchWithLogger as typeof fetch, }, plugins: [ @@ -81,7 +94,7 @@ export const authClient = createAuthClient({ adminClient(), organizationClient(), expoClient({ - storage: SecureStore + storage: storage as any }) ], }); diff --git a/lib/fetch-logger.ts b/lib/fetch-logger.ts index 95a5710..c3f6a34 100644 --- a/lib/fetch-logger.ts +++ b/lib/fetch-logger.ts @@ -1,5 +1,6 @@ import { router } from 'expo-router' import * as SecureStore from 'expo-secure-store' +import { storage } from './storage'; interface FetchLoggerOptions { enableLogging?: boolean; @@ -26,7 +27,7 @@ export const createFetchWithLogger = (options: FetchLoggerOptions = {}) => { const method = init?.method || 'GET'; try { - const token = await SecureStore.getItemAsync('token'); + const token = await storage.getItem('token'); if (token) { init = { diff --git a/storage.native.ts b/lib/storage.native.ts similarity index 100% rename from storage.native.ts rename to lib/storage.native.ts diff --git a/storage.ts b/lib/storage.ts similarity index 94% rename from storage.ts rename to lib/storage.ts index a06f838..73c788e 100644 --- a/storage.ts +++ b/lib/storage.ts @@ -1,3 +1,4 @@ +declare const window: any; export const storage = { async getItem(key: string): Promise { if (typeof window === "undefined") return null;