Files
gridpilot.gg/core/identity/application/use-cases/HandleAuthCallbackUseCase.ts
2025-12-21 01:20:27 +01:00

57 lines
2.0 KiB
TypeScript

import type { AuthCallbackCommandDTO } from '../dto/AuthCallbackCommandDTO';
import type { AuthSessionDTO } from '../dto/AuthSessionDTO';
import type { AuthenticatedUserDTO } from '../dto/AuthenticatedUserDTO';
import type { IdentityProviderPort } from '../ports/IdentityProviderPort';
import type { IdentitySessionPort } from '../ports/IdentitySessionPort';
import { Result } from '@core/shared/application/Result';
import type { ApplicationErrorCode } from '@core/shared/errors/ApplicationErrorCode';
import type { UseCaseOutputPort, Logger } from '@core/shared/application';
export type HandleAuthCallbackInput = AuthCallbackCommandDTO;
export type HandleAuthCallbackResult = AuthSessionDTO;
export type HandleAuthCallbackErrorCode = 'REPOSITORY_ERROR';
export type HandleAuthCallbackApplicationError = ApplicationErrorCode<
HandleAuthCallbackErrorCode,
{ message: string }
>;
export class HandleAuthCallbackUseCase {
constructor(
private readonly provider: IdentityProviderPort,
private readonly sessionPort: IdentitySessionPort,
private readonly logger: Logger,
private readonly output: UseCaseOutputPort<HandleAuthCallbackResult>,
) {}
async execute(input: HandleAuthCallbackInput): Promise<
Result<void, HandleAuthCallbackApplicationError>
> {
try {
const user: AuthenticatedUserDTO = await this.provider.completeAuth(input);
const session = await this.sessionPort.createSession(user);
this.output.present(session);
return Result.ok(undefined);
} catch (error) {
const message =
error instanceof Error && error.message
? error.message
: 'Failed to execute HandleAuthCallbackUseCase';
this.logger.error(
'HandleAuthCallbackUseCase.execute failed',
error instanceof Error ? error : undefined,
{ input },
);
return Result.err({
code: 'REPOSITORY_ERROR',
details: { message },
} as HandleAuthCallbackApplicationError);
}
}
}