From 98643fee397afe7c301271a5be078b3862d69136 Mon Sep 17 00:00:00 2001 From: Sergey Krylov Date: Fri, 20 Feb 2026 06:41:01 +0300 Subject: [PATCH] feat: add telegram authorization --- src/modules/auth/auth.controller.ts | 27 ++++++++++++++++++- src/modules/auth/auth.grpc.ts | 5 ++++ src/modules/auth/dto/rq/index.ts | 1 + .../dto/rq/telegram-finalyze.request.dto.ts | 12 +++++++++ yarn.lock | 6 ++--- 5 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 src/modules/auth/dto/rq/telegram-finalyze.request.dto.ts diff --git a/src/modules/auth/auth.controller.ts b/src/modules/auth/auth.controller.ts index 87d0522..5a97aef 100644 --- a/src/modules/auth/auth.controller.ts +++ b/src/modules/auth/auth.controller.ts @@ -6,7 +6,8 @@ import { HttpStatus, Post, Req, - Res, UnauthorizedException + Res, + UnauthorizedException } from '@nestjs/common' import { ConfigService } from '@nestjs/config' import { ApiOperation } from '@nestjs/swagger' @@ -16,6 +17,7 @@ import { lastValueFrom } from 'rxjs' import { AuthClientGrpc } from './auth.grpc' import { SendOtpRequestDto, + TelegramFinalyzeRequestDto, TelegramVerifyDto, VerifyOtpRequestDto } from './dto' @@ -137,4 +139,27 @@ export class AuthController { throw new UnauthorizedException('Invalid telegram login response') } + + @Post('telegram/finalize') + @HttpCode(HttpStatus.OK) + public async finalyzeTelegramLogin( + @Body() dto: TelegramFinalyzeRequestDto, + @Res({ passthrough: true }) res: Response + ) { + const { sessionId } = dto + + const { accessToken, refreshToken } = await lastValueFrom( + this.client.telegramConsume({ sessionId }) + ) + + res.cookie('refreshToken', refreshToken, { + httpOnly: true, + secure: process.env.NODE_ENV === 'production', + domain: this.config.getOrThrow('COOKIES_DOMAIN'), + sameSite: 'lax', + maxAge: 30 * 24 * 60 * 60 * 1000 + }) + + return { accessToken } + } } diff --git a/src/modules/auth/auth.grpc.ts b/src/modules/auth/auth.grpc.ts index 79272fc..1a33433 100644 --- a/src/modules/auth/auth.grpc.ts +++ b/src/modules/auth/auth.grpc.ts @@ -4,6 +4,7 @@ import { AuthServiceClient, RefreshRequest, SendOtpRequest, + TelegramConsumeRequest, TelegramVerifyRequest, VerifyOtpRequest } from '@teacinema/contracts/gen/auth' @@ -37,4 +38,8 @@ export class AuthClientGrpc implements OnModuleInit { public telegramVerify(request: TelegramVerifyRequest) { return this.authService.telegramVerify(request) } + + public telegramConsume(request: TelegramConsumeRequest) { + return this.authService.telegramConsume(request) + } } diff --git a/src/modules/auth/dto/rq/index.ts b/src/modules/auth/dto/rq/index.ts index 000008f..3c82312 100644 --- a/src/modules/auth/dto/rq/index.ts +++ b/src/modules/auth/dto/rq/index.ts @@ -1,3 +1,4 @@ export * from './send-otp.request.dto' export * from './verify-otp.request.dto' export * from './telegram-verify.dto' +export * from './telegram-finalyze.request.dto' diff --git a/src/modules/auth/dto/rq/telegram-finalyze.request.dto.ts b/src/modules/auth/dto/rq/telegram-finalyze.request.dto.ts new file mode 100644 index 0000000..8312c22 --- /dev/null +++ b/src/modules/auth/dto/rq/telegram-finalyze.request.dto.ts @@ -0,0 +1,12 @@ +import { ApiProperty } from '@nestjs/swagger' +import { IsNotEmpty, IsString } from 'class-validator' + +export class TelegramFinalyzeRequestDto { + @ApiProperty({ + description: 'Telegram session id', + example: '4399b5e099fb40a8e6e8a4ca5120e43c' + }) + @IsString() + @IsNotEmpty() + sessionId: string +} diff --git a/yarn.lock b/yarn.lock index 77c56a0..52b5d01 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1221,9 +1221,9 @@ integrity sha512-q3DURJbSk3k8MNWFIYaSM4LEcBgPbWa+HJmBz/nzYT4kuYitJVSXxpZ97kr0Ea+81AZwAU3JhQKlkK0SBWUi0A== "@teacinema/contracts@^1.0.0": - version "1.0.8" - resolved "https://git.ksv741.keenetic.pro/api/packages/teacinema/npm/%40teacinema%2Fcontracts/-/1.0.8/contracts-1.0.8.tgz#4e31c3a370b0808629d9c93900c5f27958ef82e6" - integrity sha512-xyPyifbVwWelgo4MsvhhRl//To+b0Tnp8DtANCzwLBUhwPj3ZG19QgfPB7Ih7PsBU5TD6F80aNlITDxJbFgkFQ== + version "1.0.9" + resolved "https://git.ksv741.keenetic.pro/api/packages/teacinema/npm/%40teacinema%2Fcontracts/-/1.0.9/contracts-1.0.9.tgz#14b136d0c08552147b0de637a7af6d340d538527" + integrity sha512-9BwWvXaDKgRcef6+1ep9vUDWBdOndqheeTX5mXK5OOvLXbLOlzYoqAUoT0ZwaRwOCD+Je/LDrqcdti6eoss71Q== dependencies: "@nestjs/microservices" "^11.1.12" protoc "33.4.0"