feat: migrate auth from supabase to better auth (#596)
* feat: add README for @cio/db package and remove existingAccounts.json * load session, load profile and organization, add create profile hook * chore: cache session request The trick was passing all the cookies to the backend. Previously I was only passing the better-auth.session_token and not better-auth.session_data. Read about the difference here https://www.better-auth.com/docs/concepts/cookies#custom-cookies * feat: login, logout, onboarding, verify email, reset password. * feat: complete change email and email verification * feat: standardise how we make rpc calls and create org endpoints * fix ui errors * fix: handle potential undefined customerId and standardize avatar URL property * refactor: standardize custom domain handling and update telegram chat ID references * chore: add tailwind prefix to @cio/ui this fixed the media query issues because @cio/ui classes were overriding this dashboard tailwind build * chore: clean up org sidebar * chore: persist mode state * feat: implement order breadcrumbs * chore: upgrade to better auth 1.4 * chore: address common auth flow bugs and setup app for others to setup locally.
This commit is contained in:
@@ -0,0 +1,141 @@
|
||||
import { getErrMsg } from './utils';
|
||||
import type { InferResponseType } from '@cio/api/rpc-types';
|
||||
import { ApiError, type ZodValidationError, type ZodValidatorMesssage } from './types';
|
||||
import { mapZodErrorsToTranslations } from '$lib/utils/validation/validation';
|
||||
import { ZodError } from 'zod/v4';
|
||||
|
||||
// Extract the data type from a success response
|
||||
type ExtractData<T> = Extract<InferResponseType<T>, { success: true }>;
|
||||
type ExtractError<T> = Extract<InferResponseType<T>, { success: false }>;
|
||||
|
||||
interface ExecuteOptions<TrpcType> {
|
||||
requestFn: () => Promise<Response>;
|
||||
errorMessage?: string;
|
||||
onSuccess?: (data: ExtractData<TrpcType>) => Promise<void> | void;
|
||||
onError?: (error: ExtractError<TrpcType> | string) => Promise<void> | void;
|
||||
logContext?: string;
|
||||
}
|
||||
|
||||
/**
|
||||
* Base API class that provides shared state management and request handling
|
||||
* for all API classes in the application.
|
||||
*/
|
||||
export class BaseApi {
|
||||
isLoading = $state(false);
|
||||
error = $state<string | null>(null);
|
||||
|
||||
/**
|
||||
* Execute an API request with automatic loading and error state management
|
||||
* Type is automatically inferred from the RPC call (e.g., classroomio.organization.plan.$post)
|
||||
* @param options Configuration for the request
|
||||
* @returns The data from the response, or the default return value on error
|
||||
*/
|
||||
protected async execute<TrpcType>(options: ExecuteOptions<TrpcType>): Promise<ExtractData<TrpcType> | undefined> {
|
||||
const { requestFn, onSuccess, logContext = 'API request' } = options;
|
||||
|
||||
this.isLoading = true;
|
||||
this.error = null;
|
||||
|
||||
try {
|
||||
const response = await requestFn();
|
||||
const result = (await response.json()) as unknown;
|
||||
|
||||
if (typeof result === 'object' && result !== null && 'success' in result) {
|
||||
const apiResult = result as { success: boolean; data?: unknown; error?: string; message?: string };
|
||||
|
||||
if (apiResult.success === false) {
|
||||
console.log('apiResult false', apiResult);
|
||||
options.onError?.(apiResult as ExtractError<TrpcType>);
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
const typedResult = apiResult as ExtractData<TrpcType>;
|
||||
|
||||
this.resetErrors();
|
||||
|
||||
if (onSuccess) {
|
||||
await onSuccess(typedResult);
|
||||
}
|
||||
|
||||
return typedResult;
|
||||
}
|
||||
} catch (error) {
|
||||
console.error(`Error in ${logContext}:`, error);
|
||||
this.error = getErrMsg(error, `Error ${logContext}:`);
|
||||
|
||||
const errorResult = this.getServerError<ExtractError<TrpcType>>(error);
|
||||
options.onError?.(errorResult);
|
||||
} finally {
|
||||
this.isLoading = false;
|
||||
}
|
||||
}
|
||||
|
||||
getServerError<T>(error: unknown): T | string {
|
||||
if (error instanceof ApiError) {
|
||||
console.log('instance of ApiError', error);
|
||||
try {
|
||||
const json = JSON.parse(error.message) as T;
|
||||
return json;
|
||||
} catch (e) {
|
||||
console.log('error parsing json', e);
|
||||
return error.message;
|
||||
}
|
||||
}
|
||||
|
||||
if (error instanceof Error) {
|
||||
return error.message;
|
||||
}
|
||||
|
||||
if (typeof error === 'string') {
|
||||
return error;
|
||||
}
|
||||
|
||||
return `${error}`;
|
||||
}
|
||||
|
||||
resetErrors() {
|
||||
this.error = null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the API state
|
||||
*/
|
||||
reset() {
|
||||
this.isLoading = false;
|
||||
this.error = null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Extended base API class that supports errors object (Record<string, string>)
|
||||
* for form validation scenarios
|
||||
*/
|
||||
export class BaseApiWithErrors extends BaseApi {
|
||||
errors = $state<Record<string, string>>({});
|
||||
success = $state(false);
|
||||
|
||||
handleValidationError(result: { success: false; error: string; code: string; field?: string }): Promise<void> | void {
|
||||
const zodValidationError = result.error as unknown as ZodValidationError;
|
||||
if (zodValidationError.name === 'ZodError') {
|
||||
const issues = JSON.parse(zodValidationError.message) as ZodValidatorMesssage;
|
||||
|
||||
this.errors = mapZodErrorsToTranslations(new ZodError(issues));
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
override resetErrors() {
|
||||
this.errors = {};
|
||||
}
|
||||
|
||||
/**
|
||||
* Reset the API state including errors and success
|
||||
*/
|
||||
override reset() {
|
||||
super.reset();
|
||||
this.errors = {};
|
||||
this.success = false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user