feat: add telegram verification
This commit is contained in:
parent
2ca5ddec9d
commit
13e4937925
@ -1,11 +1,12 @@
|
||||
import {
|
||||
Body,
|
||||
Controller,
|
||||
Get,
|
||||
HttpCode,
|
||||
HttpStatus,
|
||||
Post,
|
||||
Req,
|
||||
Res
|
||||
Res, UnauthorizedException
|
||||
} from '@nestjs/common'
|
||||
import { ConfigService } from '@nestjs/config'
|
||||
import { ApiOperation } from '@nestjs/swagger'
|
||||
@ -13,7 +14,11 @@ import type { Request, Response } from 'express'
|
||||
import { lastValueFrom } from 'rxjs'
|
||||
|
||||
import { AuthClientGrpc } from './auth.grpc'
|
||||
import { SendOtpRequestDto, VerifyOtpRequestDto } from './dto'
|
||||
import {
|
||||
SendOtpRequestDto,
|
||||
TelegramVerifyDto,
|
||||
VerifyOtpRequestDto
|
||||
} from './dto'
|
||||
|
||||
@Controller('auth')
|
||||
export class AuthController {
|
||||
@ -94,4 +99,42 @@ export class AuthController {
|
||||
res.clearCookie('refreshToken')
|
||||
return { ok: true }
|
||||
}
|
||||
|
||||
@Get('telegram/init')
|
||||
@HttpCode(HttpStatus.OK)
|
||||
public telegramInit() {
|
||||
return this.client.telegramInit()
|
||||
}
|
||||
|
||||
@Post('telegram/verify')
|
||||
@HttpCode(HttpStatus.OK)
|
||||
public async telegramVerify(
|
||||
@Body() dto: TelegramVerifyDto,
|
||||
@Res({ passthrough: true }) res: Response
|
||||
) {
|
||||
const query = JSON.parse(atob(dto.tgAuthResult)) as {
|
||||
[key: string]: string
|
||||
}
|
||||
const result = await lastValueFrom(this.client.telegramVerify({ query }))
|
||||
|
||||
if (result.url) {
|
||||
return result
|
||||
}
|
||||
|
||||
if (result.accessToken && result.refreshToken) {
|
||||
const { accessToken, refreshToken } = result
|
||||
|
||||
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 }
|
||||
}
|
||||
|
||||
throw new UnauthorizedException('Invalid telegram login response')
|
||||
}
|
||||
}
|
||||
|
||||
@ -4,6 +4,7 @@ import {
|
||||
AuthServiceClient,
|
||||
RefreshRequest,
|
||||
SendOtpRequest,
|
||||
TelegramVerifyRequest,
|
||||
VerifyOtpRequest
|
||||
} from '@teacinema/contracts/gen/auth'
|
||||
|
||||
@ -28,4 +29,12 @@ export class AuthClientGrpc implements OnModuleInit {
|
||||
public refresh(request: RefreshRequest) {
|
||||
return this.authService.refresh(request)
|
||||
}
|
||||
|
||||
public telegramInit() {
|
||||
return this.authService.telegramInit({})
|
||||
}
|
||||
|
||||
public telegramVerify(request: TelegramVerifyRequest) {
|
||||
return this.authService.telegramVerify(request)
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,2 +1,3 @@
|
||||
export * from './send-otp.request.dto'
|
||||
export * from './verify-otp.request.dto'
|
||||
export * from './telegram-verify.dto'
|
||||
|
||||
7
src/modules/auth/dto/rq/telegram-verify.dto.ts
Normal file
7
src/modules/auth/dto/rq/telegram-verify.dto.ts
Normal file
@ -0,0 +1,7 @@
|
||||
import { IsNotEmpty, IsString } from 'class-validator'
|
||||
|
||||
export class TelegramVerifyDto {
|
||||
@IsString()
|
||||
@IsNotEmpty()
|
||||
tgAuthResult: string
|
||||
}
|
||||
@ -1221,9 +1221,9 @@
|
||||
integrity sha512-q3DURJbSk3k8MNWFIYaSM4LEcBgPbWa+HJmBz/nzYT4kuYitJVSXxpZ97kr0Ea+81AZwAU3JhQKlkK0SBWUi0A==
|
||||
|
||||
"@teacinema/contracts@^1.0.0":
|
||||
version "1.0.7"
|
||||
resolved "https://git.ksv741.keenetic.pro/api/packages/teacinema/npm/%40teacinema%2Fcontracts/-/1.0.7/contracts-1.0.7.tgz#2c75a3118e4126d30f5c8193c4518bac3f1812ff"
|
||||
integrity sha512-lE14PO/yBphYCu0BGFGyGAyvJ74v+ACz34MXA3fFo/PwThPVpudBReSG6oRyUACRybkN882GKXFU6LrJXvGbrQ==
|
||||
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==
|
||||
dependencies:
|
||||
"@nestjs/microservices" "^11.1.12"
|
||||
protoc "33.4.0"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user