92 lines
3.5 KiB
TypeScript
92 lines
3.5 KiB
TypeScript
import { getWebsiteApiBaseUrl } from '@/lib/config/apiBaseUrl';
|
|
import { isProductionEnvironment } from '@/lib/config/env';
|
|
import { Result } from '@/lib/contracts/Result';
|
|
import { DomainError, Service } from '@/lib/contracts/services/Service';
|
|
import { AuthApiClient } from '@/lib/gateways/api/auth/AuthApiClient';
|
|
import { EnhancedErrorReporter } from '@/lib/infrastructure/EnhancedErrorReporter';
|
|
import { ConsoleLogger } from '@/lib/infrastructure/logging/ConsoleLogger';
|
|
import type { LoginParamsDTO } from '@/lib/types/generated/LoginParamsDTO';
|
|
import type { ResetPasswordDTO } from '@/lib/types/generated/ResetPasswordDTO';
|
|
import type { SignupParamsDTO } from '@/lib/types/generated/SignupParamsDTO';
|
|
import { SessionViewModel } from '@/lib/view-models/SessionViewModel';
|
|
import { injectable, unmanaged } from 'inversify';
|
|
|
|
/**
|
|
* Auth Service
|
|
*
|
|
* Orchestrates authentication operations.
|
|
* Returns raw API DTOs. No ViewModels or UX logic.
|
|
*/
|
|
@injectable()
|
|
export class AuthService implements Service {
|
|
private apiClient: AuthApiClient;
|
|
|
|
constructor(@unmanaged() 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<SessionViewModel, DomainError>> {
|
|
try {
|
|
const dto = await this.apiClient.login(params);
|
|
return Result.ok(new SessionViewModel(dto.user));
|
|
} catch (error: unknown) {
|
|
return Result.err({ type: 'unauthorized', message: (error as Error).message || 'Login failed' });
|
|
}
|
|
}
|
|
|
|
async signup(params: SignupParamsDTO): Promise<Result<SessionViewModel, DomainError>> {
|
|
try {
|
|
const dto = await this.apiClient.signup(params);
|
|
return Result.ok(new SessionViewModel(dto.user));
|
|
} catch (error: unknown) {
|
|
return Result.err({ type: 'validation', message: (error as Error).message || 'Signup failed' });
|
|
}
|
|
}
|
|
|
|
async logout(): Promise<Result<void, DomainError>> {
|
|
try {
|
|
await this.apiClient.logout();
|
|
return Result.ok(undefined);
|
|
} catch (error: unknown) {
|
|
return Result.err({ type: 'serverError', message: (error as Error).message || 'Logout failed' });
|
|
}
|
|
}
|
|
|
|
async forgotPassword(params: ForgotPasswordDTO): Promise<Result<{ message: string; magicLink?: string }, DomainError>> {
|
|
try {
|
|
const result = await this.apiClient.forgotPassword(params);
|
|
return Result.ok(result);
|
|
} catch (error: unknown) {
|
|
return Result.err({ type: 'serverError', message: (error as Error).message || 'Forgot password request failed' });
|
|
}
|
|
}
|
|
|
|
async resetPassword(params: ResetPasswordDTO): Promise<Result<{ message: string }, DomainError>> {
|
|
try {
|
|
const result = await this.apiClient.resetPassword(params);
|
|
return Result.ok(result);
|
|
} catch (error: unknown) {
|
|
return Result.err({ type: 'serverError', message: (error as Error).message || 'Reset password failed' });
|
|
}
|
|
}
|
|
|
|
async getSession(): Promise<Result<any, DomainError>> {
|
|
try {
|
|
const dto = await this.apiClient.getSession();
|
|
return Result.ok(dto);
|
|
} catch (error: unknown) {
|
|
return Result.err({ type: 'serverError', message: (error as Error).message || 'Failed to get session' });
|
|
}
|
|
}
|
|
} |