72 lines
1.8 KiB
TypeScript
72 lines
1.8 KiB
TypeScript
import { AuthApiClient } from '../../api/auth/AuthApiClient';
|
|
import { SessionViewModel } from '../../view-models/SessionViewModel';
|
|
import type { LoginParams } from '../../types/generated/LoginParams';
|
|
import type { SignupParams } from '../../types/generated/SignupParams';
|
|
import type { LoginWithIracingCallbackParams } from '../../types/generated/LoginWithIracingCallbackParams';
|
|
|
|
/**
|
|
* 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: SignupParams): 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: LoginParams): 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;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Get iRacing authentication URL
|
|
*/
|
|
getIracingAuthUrl(returnTo?: string): string {
|
|
return this.apiClient.getIracingAuthUrl(returnTo);
|
|
}
|
|
|
|
/**
|
|
* Login with iRacing callback
|
|
*/
|
|
async loginWithIracingCallback(params: LoginWithIracingCallbackParams): Promise<SessionViewModel> {
|
|
try {
|
|
const dto = await this.apiClient.loginWithIracingCallback(params);
|
|
return new SessionViewModel(dto.user);
|
|
} catch (error) {
|
|
throw error;
|
|
}
|
|
}
|
|
}
|