Files
gridpilot.gg/apps/website/lib/auth/InMemoryAuthService.ts
2025-12-04 15:49:57 +01:00

56 lines
2.3 KiB
TypeScript

import type { AuthService, AuthSession } from './AuthService';
import type { AuthCallbackCommandDTO } from '@gridpilot/identity/application/dto/AuthCallbackCommandDTO';
import type { StartAuthCommandDTO } from '@gridpilot/identity/application/dto/StartAuthCommandDTO';
import { StartAuthUseCase } from '@gridpilot/identity/application/use-cases/StartAuthUseCase';
import { GetCurrentUserSessionUseCase } from '@gridpilot/identity/application/use-cases/GetCurrentUserSessionUseCase';
import { HandleAuthCallbackUseCase } from '@gridpilot/identity/application/use-cases/HandleAuthCallbackUseCase';
import { LogoutUseCase } from '@gridpilot/identity/application/use-cases/LogoutUseCase';
import { CookieIdentitySessionAdapter } from '@gridpilot/identity/infrastructure/session/CookieIdentitySessionAdapter';
import { IracingDemoIdentityProviderAdapter } from '@gridpilot/identity/infrastructure/providers/IracingDemoIdentityProviderAdapter';
export class InMemoryAuthService implements AuthService {
async getCurrentSession(): Promise<AuthSession | null> {
const sessionPort = new CookieIdentitySessionAdapter();
const useCase = new GetCurrentUserSessionUseCase(sessionPort);
return useCase.execute();
}
async startIracingAuthRedirect(
returnTo?: string,
): Promise<{ redirectUrl: string; state: string }> {
const provider = new IracingDemoIdentityProviderAdapter();
const useCase = new StartAuthUseCase(provider);
const command: StartAuthCommandDTO = {
provider: 'IRACING_DEMO',
returnTo,
};
return useCase.execute(command);
}
async loginWithIracingCallback(params: {
code: string;
state: string;
returnTo?: string;
}): Promise<AuthSession> {
const provider = new IracingDemoIdentityProviderAdapter();
const sessionPort = new CookieIdentitySessionAdapter();
const useCase = new HandleAuthCallbackUseCase(provider, sessionPort);
const command: AuthCallbackCommandDTO = {
provider: 'IRACING_DEMO',
code: params.code,
state: params.state,
returnTo: params.returnTo,
};
return useCase.execute(command);
}
async logout(): Promise<void> {
const sessionPort = new CookieIdentitySessionAdapter();
const useCase = new LogoutUseCase(sessionPort);
await useCase.execute();
}
}