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 { ScrollView, Dimensions, TextInput, Platform } from 'react-native'
|
||||||
import { useAuth } from '@/hooks/core/use-auth'
|
import { useAuth } from '@/hooks/core/use-auth'
|
||||||
import { KeyboardAvoidingView } from 'react-native-keyboard-controller'
|
import { KeyboardAvoidingView } from 'react-native-keyboard-controller'
|
||||||
|
import { setAuthToken } from '@/lib/auth'
|
||||||
|
|
||||||
|
|
||||||
const BACKGROUND_VIDEOS = [
|
const BACKGROUND_VIDEOS = [
|
||||||
|
|
@ -40,6 +41,16 @@ export default function Auth() {
|
||||||
const result = await signIn.username({
|
const result = await signIn.username({
|
||||||
username,
|
username,
|
||||||
password,
|
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) {
|
if (result.error) {
|
||||||
|
|
@ -80,7 +91,7 @@ export default function Auth() {
|
||||||
})
|
})
|
||||||
|
|
||||||
console.log('result------------', result);
|
console.log('result------------', result);
|
||||||
|
|
||||||
if (result.error) {
|
if (result.error) {
|
||||||
Toast.show({ title: result.error.message || '注册失败' })
|
Toast.show({ title: result.error.message || '注册失败' })
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -102,7 +113,7 @@ export default function Auth() {
|
||||||
}, [mode, handleLogin, handleRegister])
|
}, [mode, handleLogin, handleRegister])
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<KeyboardAvoidingView behavior="padding" className="flex-1">
|
<KeyboardAvoidingView behavior="padding" className="flex-1">
|
||||||
<Block className="relative flex-1 bg-black">
|
<Block className="relative flex-1 bg-black">
|
||||||
<Block className="absolute inset-[0px] z-[0] overflow-hidden">
|
<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 }} />
|
<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 "reflect-metadata";
|
||||||
import { createAuthClient } from "better-auth/react";
|
import { createAuthClient } from "better-auth/react";
|
||||||
import { usernameClient, phoneNumberClient, emailOTPClient, genericOAuthClient, adminClient, organizationClient, jwtClient } from 'better-auth/client/plugins'
|
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 { expoClient } from "@better-auth/expo/client";
|
||||||
import { createSkerClientPlugin } from '@repo/sdk'
|
import { createSkerClientPlugin } from '@repo/sdk'
|
||||||
import { stripeClient } from './plugins/stripe-plugin';
|
import { stripeClient } from './plugins/stripe-plugin';
|
||||||
import type { Subscription } from './plugins/stripe';
|
import type { Subscription } from './plugins/stripe';
|
||||||
import type { ApiError } from './types';
|
import type { ApiError } from './types';
|
||||||
import { fetchWithLogger } from './fetch-logger';
|
import { fetchWithLogger } from './fetch-logger';
|
||||||
|
import { storage } from './storage';
|
||||||
|
|
||||||
export interface CreditBalance {
|
export interface CreditBalance {
|
||||||
tokenUsage: number;
|
tokenUsage: number;
|
||||||
|
|
@ -62,12 +62,25 @@ export interface ISubscription {
|
||||||
restore: (params?: SubscriptionRestoreParams) => Promise<{ data?: SubscriptionRestoreResponse; error?: ApiError }>;
|
restore: (params?: SubscriptionRestoreParams) => Promise<{ data?: SubscriptionRestoreResponse; error?: ApiError }>;
|
||||||
billingPortal: (params?: BillingPortalParams) => Promise<{ data?: BillingPortalResponse; 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({
|
export const authClient = createAuthClient({
|
||||||
baseURL: "https://api-test.mixvideo.bowong.cc",
|
baseURL: "https://api-test.mixvideo.bowong.cc",
|
||||||
trustedOrigins: ["duooomi://", "https://api-test.mixvideo.bowong.cc"],
|
trustedOrigins: ["duooomi://", "https://api-test.mixvideo.bowong.cc"],
|
||||||
storage: SecureStore,
|
storage,
|
||||||
scheme: "duooomi",
|
scheme: "duooomi",
|
||||||
fetchOptions: {
|
fetchOptions: {
|
||||||
|
auth: {
|
||||||
|
type: 'Bearer',
|
||||||
|
token: async () => {
|
||||||
|
const Authorization = await getAuthToken()
|
||||||
|
return Authorization;
|
||||||
|
}
|
||||||
|
},
|
||||||
customFetchImpl: fetchWithLogger as typeof fetch,
|
customFetchImpl: fetchWithLogger as typeof fetch,
|
||||||
},
|
},
|
||||||
plugins: [
|
plugins: [
|
||||||
|
|
@ -81,7 +94,7 @@ export const authClient = createAuthClient({
|
||||||
adminClient(),
|
adminClient(),
|
||||||
organizationClient(),
|
organizationClient(),
|
||||||
expoClient({
|
expoClient({
|
||||||
storage: SecureStore
|
storage: storage as any
|
||||||
})
|
})
|
||||||
],
|
],
|
||||||
});
|
});
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,6 @@
|
||||||
import { router } from 'expo-router'
|
import { router } from 'expo-router'
|
||||||
import * as SecureStore from 'expo-secure-store'
|
import * as SecureStore from 'expo-secure-store'
|
||||||
|
import { storage } from './storage';
|
||||||
|
|
||||||
interface FetchLoggerOptions {
|
interface FetchLoggerOptions {
|
||||||
enableLogging?: boolean;
|
enableLogging?: boolean;
|
||||||
|
|
@ -26,7 +27,7 @@ export const createFetchWithLogger = (options: FetchLoggerOptions = {}) => {
|
||||||
const method = init?.method || 'GET';
|
const method = init?.method || 'GET';
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const token = await SecureStore.getItemAsync('token');
|
const token = await storage.getItem('token');
|
||||||
|
|
||||||
if (token) {
|
if (token) {
|
||||||
init = {
|
init = {
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,4 @@
|
||||||
|
declare const window: any;
|
||||||
export const storage = {
|
export const storage = {
|
||||||
async getItem(key: string): Promise<string | null> {
|
async getItem(key: string): Promise<string | null> {
|
||||||
if (typeof window === "undefined") return null;
|
if (typeof window === "undefined") return null;
|
||||||
Loading…
Reference in New Issue