expo-duooomi-app/lib/auth.ts

96 lines
3.0 KiB
TypeScript

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';
export interface CreditBalance {
tokenUsage: number;
remainingTokenBalance: number;
currentCreditBalance: number;
unitPrice: number;
}
export interface SubscriptionListItem extends Subscription {
type?: 'metered' | 'licenced';
limits?: Record<string, number>;
creditBalance?: CreditBalance;
highestTierPlan?: string;
highestTierCredits?: number;
}
export interface SubscriptionListParams {
query?: {
referenceId?: string;
};
}
export interface SubscriptionRestoreParams {
referenceId?: string;
subscriptionId?: string;
}
export interface SubscriptionRestoreResponse {
success: boolean;
subscription: {
id: string;
stripeSubscriptionId: string;
cancelAtPeriodEnd: boolean;
periodEnd?: Date;
status: string;
};
}
export interface BillingPortalParams {
locale?: string;
referenceId?: string;
returnUrl?: string;
}
export interface BillingPortalResponse {
url: string;
redirect: boolean;
}
export interface ISubscription {
list: (params?: SubscriptionListParams) => Promise<{ data?: SubscriptionListItem[]; error?: ApiError }>;
restore: (params?: SubscriptionRestoreParams) => Promise<{ data?: SubscriptionRestoreResponse; error?: ApiError }>;
billingPortal: (params?: BillingPortalParams) => Promise<{ data?: BillingPortalResponse; error?: ApiError }>;
}
export const authClient = createAuthClient({
baseURL: "https://api-test.mixvideo.bowong.cc",
trustedOrigins: ["duooomi://", "https://api-test.mixvideo.bowong.cc"],
storage: SecureStore,
scheme: "duooomi",
fetchOptions: {
customFetchImpl: fetchWithLogger as typeof fetch,
},
plugins: [
createSkerClientPlugin(),
stripeClient(),
usernameClient(),
phoneNumberClient(),
emailOTPClient(),
genericOAuthClient(),
jwtClient(),
adminClient(),
organizationClient(),
expoClient({
storage: SecureStore
})
],
});
export const { signIn, signUp, signOut, useSession, $Infer, admin } = authClient;
export const subscription: ISubscription = Reflect.get(authClient, 'subscription');
export interface IAlipay {
appPay(orderInfo: any): Promise<{ data: any, error?: any }>;
[`app-pay`](orderInfo: any): Promise<{ data: any, error?: any }>;
}
export const alipay: IAlipay = Reflect.get(authClient, 'alipay');