website refactor
This commit is contained in:
@@ -10,6 +10,7 @@ import type { SignupParamsDTO } from '@/lib/types/generated/SignupParamsDTO';
|
||||
import type { ForgotPasswordDTO } from '@/lib/types/generated/ForgotPasswordDTO';
|
||||
import type { ResetPasswordDTO } from '@/lib/types/generated/ResetPasswordDTO';
|
||||
import { isProductionEnvironment } from '@/lib/config/env';
|
||||
import { SessionViewModel } from '@/lib/view-models/SessionViewModel';
|
||||
|
||||
/**
|
||||
* Auth Service
|
||||
@@ -20,41 +21,45 @@ import { isProductionEnvironment } from '@/lib/config/env';
|
||||
export class AuthService implements Service {
|
||||
private apiClient: AuthApiClient;
|
||||
|
||||
constructor() {
|
||||
const baseUrl = getWebsiteApiBaseUrl();
|
||||
const logger = new ConsoleLogger();
|
||||
const errorReporter = new EnhancedErrorReporter(logger, {
|
||||
showUserNotifications: false,
|
||||
logToConsole: true,
|
||||
reportToExternal: isProductionEnvironment(),
|
||||
});
|
||||
this.apiClient = new AuthApiClient(baseUrl, errorReporter, logger);
|
||||
constructor(apiClient?: AuthApiClient) {
|
||||
if (apiClient) {
|
||||
this.apiClient = apiClient;
|
||||
} else {
|
||||
const baseUrl = getWebsiteApiBaseUrl();
|
||||
const logger = new ConsoleLogger();
|
||||
const errorReporter = new EnhancedErrorReporter(logger, {
|
||||
showUserNotifications: false,
|
||||
logToConsole: true,
|
||||
reportToExternal: isProductionEnvironment(),
|
||||
});
|
||||
this.apiClient = new AuthApiClient(baseUrl, errorReporter, logger);
|
||||
}
|
||||
}
|
||||
|
||||
async login(params: LoginParamsDTO): Promise<Result<AuthSessionDTO, DomainError>> {
|
||||
async login(params: LoginParamsDTO): Promise<any> {
|
||||
try {
|
||||
const dto = await this.apiClient.login(params);
|
||||
return Result.ok(dto);
|
||||
return new SessionViewModel(dto.user);
|
||||
} catch (error: unknown) {
|
||||
return Result.err({ type: 'unauthorized', message: (error as Error).message || 'Login failed' });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async signup(params: SignupParamsDTO): Promise<Result<AuthSessionDTO, DomainError>> {
|
||||
async signup(params: SignupParamsDTO): Promise<any> {
|
||||
try {
|
||||
const dto = await this.apiClient.signup(params);
|
||||
return Result.ok(dto);
|
||||
return new SessionViewModel(dto.user);
|
||||
} catch (error: unknown) {
|
||||
return Result.err({ type: 'serverError', message: (error as Error).message || 'Signup failed' });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
async logout(): Promise<Result<void, DomainError>> {
|
||||
async logout(): Promise<any> {
|
||||
try {
|
||||
await this.apiClient.logout();
|
||||
return Result.ok(undefined);
|
||||
} catch (error: unknown) {
|
||||
return Result.err({ type: 'serverError', message: (error as Error).message || 'Logout failed' });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -76,12 +81,12 @@ export class AuthService implements Service {
|
||||
}
|
||||
}
|
||||
|
||||
async getSession(): Promise<Result<AuthSessionDTO | null, DomainError>> {
|
||||
async getSession(): Promise<any> {
|
||||
try {
|
||||
const dto = await this.apiClient.getSession();
|
||||
return Result.ok(dto);
|
||||
return dto;
|
||||
} catch (error: unknown) {
|
||||
return Result.err({ type: 'serverError', message: (error as Error).message || 'Failed to fetch session' });
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import { Result } from '@/lib/contracts/Result';
|
||||
import { DomainError, Service } from '@/lib/contracts/services/Service';
|
||||
import { AuthService } from './AuthService';
|
||||
import type { AuthSessionDTO } from '@/lib/types/generated/AuthSessionDTO';
|
||||
import { SessionViewModel } from '@/lib/view-models/SessionViewModel';
|
||||
|
||||
/**
|
||||
* Session Service
|
||||
@@ -12,14 +13,22 @@ import type { AuthSessionDTO } from '@/lib/types/generated/AuthSessionDTO';
|
||||
export class SessionService implements Service {
|
||||
private authService: AuthService;
|
||||
|
||||
constructor() {
|
||||
this.authService = new AuthService();
|
||||
constructor(apiClient?: any) {
|
||||
this.authService = new AuthService(apiClient);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get current user session
|
||||
*/
|
||||
async getSession(): Promise<Result<AuthSessionDTO | null, DomainError>> {
|
||||
return this.authService.getSession();
|
||||
async getSession(): Promise<any> {
|
||||
try {
|
||||
const res = await this.authService.getSession();
|
||||
if (!res) return null;
|
||||
const data = (res as any).value || res;
|
||||
if (!data || !data.user) return null;
|
||||
return new SessionViewModel(data.user);
|
||||
} catch (error: unknown) {
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user