fix: web端适配
This commit is contained in:
parent
fa8cc1705f
commit
cd9ed0a734
15
app/auth.tsx
15
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 (
|
||||
<KeyboardAvoidingView behavior="padding" className="flex-1">
|
||||
<KeyboardAvoidingView behavior="padding" className="flex-1">
|
||||
<Block className="relative flex-1 bg-black">
|
||||
<Block className="absolute inset-[0px] z-[0] overflow-hidden">
|
||||
<VideoBox url={bgVideo} style={{ position: 'absolute', left: 0, right: 0, top: 0, bottom: 0, opacity: 0.4 }} />
|
||||
|
|
|
|||
19
lib/auth.ts
19
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
|
||||
})
|
||||
],
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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 = {
|
||||
|
|
|
|||
|
|
@ -1,3 +1,4 @@
|
|||
declare const window: any;
|
||||
export const storage = {
|
||||
async getItem(key: string): Promise<string | null> {
|
||||
if (typeof window === "undefined") return null;
|
||||
Loading…
Reference in New Issue