Files
gridpilot.gg/apps/website/lib/services/auth/AuthService.ts
2026-01-03 11:38:51 +01:00

75 lines
1.9 KiB
TypeScript

import { AuthApiClient } from '../../api/auth/AuthApiClient';
import { SessionViewModel } from '../../view-models/SessionViewModel';
import type { LoginParamsDTO } from '../../types/generated/LoginParamsDTO';
import type { SignupParamsDTO } from '../../types/generated/SignupParamsDTO';
import type { ForgotPasswordDTO } from '../../types/generated/ForgotPasswordDTO';
import type { ResetPasswordDTO } from '../../types/generated/ResetPasswordDTO';
/**
* Auth Service
*
* Orchestrates authentication operations by coordinating API calls.
* All dependencies are injected via constructor.
*/
export class AuthService {
constructor(
private readonly apiClient: AuthApiClient
) {}
/**
* Sign up a new user
*/
async signup(params: SignupParamsDTO): Promise<SessionViewModel> {
try {
const dto = await this.apiClient.signup(params);
return new SessionViewModel(dto.user);
} catch (error) {
throw error;
}
}
/**
* Log in an existing user
*/
async login(params: LoginParamsDTO): Promise<SessionViewModel> {
try {
const dto = await this.apiClient.login(params);
return new SessionViewModel(dto.user);
} catch (error) {
throw error;
}
}
/**
* Log out the current user
*/
async logout(): Promise<void> {
try {
await this.apiClient.logout();
} catch (error) {
throw error;
}
}
/**
* Forgot password - send reset link
*/
async forgotPassword(params: ForgotPasswordDTO): Promise<{ message: string; magicLink?: string }> {
try {
return await this.apiClient.forgotPassword(params);
} catch (error) {
throw error;
}
}
/**
* Reset password with token
*/
async resetPassword(params: ResetPasswordDTO): Promise<{ message: string }> {
try {
return await this.apiClient.resetPassword(params);
} catch (error) {
throw error;
}
}
}