From fb2e851e7e3f58c915ba1086212aa7957c3f9ed3 Mon Sep 17 00:00:00 2001 From: Sergey Krylov Date: Thu, 1 Oct 2026 20:53:00 +0300 Subject: [PATCH] refactor: share authentication result type --- src/modules/auth/application/auth.service.ts | 12 ++++++------ src/modules/auth/application/auth.types.ts | 6 ++++++ src/modules/auth/presentation/auth.controller.ts | 8 ++------ 3 files changed, 14 insertions(+), 12 deletions(-) diff --git a/src/modules/auth/application/auth.service.ts b/src/modules/auth/application/auth.service.ts index 8410ab6..6b0a2fa 100644 --- a/src/modules/auth/application/auth.service.ts +++ b/src/modules/auth/application/auth.service.ts @@ -12,7 +12,7 @@ import { HttpErrorCode } from '../../../common/error-code'; import { isPrismaErrorCode, PrismaErrorCode } from '../../../prisma/prisma-error-code'; import { PrismaService } from '../../../prisma/prisma.service'; import { AuthErrorCode } from './auth-error-code'; -import { AuthUser, RefreshPayload } from './auth.types'; +import { AuthenticationResult, AuthUser, RefreshPayload } from './auth.types'; const EXPECTED_ROTATED_SESSIONS = 1; @@ -35,7 +35,7 @@ export class AuthService { async register( email: string, password: string, - ): Promise<{ accessToken: string; refreshToken: string; user: AuthUser }> { + ): Promise { const existing = await this.prisma.user.findUnique({ where: { email }, }); @@ -64,7 +64,7 @@ export class AuthService { async login( email: string, password: string, - ): Promise<{ accessToken: string; refreshToken: string; user: AuthUser }> { + ): Promise { const user = await this.prisma.user.findUnique({ where: { email }, }); @@ -80,7 +80,7 @@ export class AuthService { async refresh( token: string, - ): Promise<{ accessToken: string; refreshToken: string; user: AuthUser }> { + ): Promise { let payload: RefreshPayload; try { payload = await this.jwt.verifyAsync(token, { @@ -151,7 +151,7 @@ export class AuthService { private async createSession( user: User, - ): Promise<{ accessToken: string; refreshToken: string; user: AuthUser }> { + ): Promise { const session = await this.prisma.session.create({ data: { id: randomUUID(), @@ -166,7 +166,7 @@ export class AuthService { private async rotateSession( session: Session & { user: User }, expectedRefreshTokenHash?: string, - ): Promise<{ accessToken: string; refreshToken: string; user: AuthUser }> { + ): Promise { const refreshToken = await this.jwt.signAsync( { sub: session.userId, diff --git a/src/modules/auth/application/auth.types.ts b/src/modules/auth/application/auth.types.ts index 6e5348e..333e4e9 100644 --- a/src/modules/auth/application/auth.types.ts +++ b/src/modules/auth/application/auth.types.ts @@ -3,6 +3,12 @@ export interface AuthUser { id: string; } +export type AuthenticationResult = { + accessToken: string; + refreshToken: string; + user: AuthUser; +}; + export interface AccessPayload { sid: string; sub: string; diff --git a/src/modules/auth/presentation/auth.controller.ts b/src/modules/auth/presentation/auth.controller.ts index 8f78326..a121898 100644 --- a/src/modules/auth/presentation/auth.controller.ts +++ b/src/modules/auth/presentation/auth.controller.ts @@ -23,7 +23,7 @@ import { import { ApiProblemResponses } from '../../../common/decorators/api-safe-error-response.decorator'; import { HttpErrorCode } from '../../../common/error-code'; import { AuthService } from '../application/auth.service'; -import { AccessPayload } from '../application/auth.types'; +import { AccessPayload, AuthenticationResult } from '../application/auth.types'; import { CredentialsDto } from './auth.dto'; import { JwtAuthGuard } from './jwt-auth.guard'; @@ -117,11 +117,7 @@ export class AuthController { private respond( response: Response, - result: { - accessToken: string; - refreshToken: string; - user: { email: string; id: string }; - }, + result: AuthenticationResult, ) { response.cookie(REFRESH_COOKIE, result.refreshToken, this.cookieOptions()); return { user: result.user, accessToken: result.accessToken };