/
Befun
/
KiizLab
Обзор
Документация
Войти
/
Befun
/
KiizLab
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
CI/CD
Аналитика
Безопасность
master
backend/src/auth/auth.controller.ts
76 строк
2 KB
BeFun
Add: profile
16 апр 2025, 11:27
16 апр 2025, 11:27
f5a455e
Код
Авторство
О чём код?
import { Body, Controller, HttpCode, Post, Req, Res, UnauthorizedException, UsePipes, ValidationPipe } from '@nestjs/common' import { Request, Response } from 'express' import { UserService } from 'src/user/user.service' import { AuthService } from './auth.service' import { AuthDto, RegisterDto } from './dto/auth.dto' @Controller('auth') export class AuthController { constructor( private readonly authService: AuthService, private readonly userService: UserService ) {} @UsePipes(new ValidationPipe()) @HttpCode(200) @Post('login') async login(@Body() dto: AuthDto, @Res({ passthrough: true }) res: Response) { const { refreshToken, ...response } = await this.authService.login(dto) this.authService.addRefreshTokenToResponse(res, refreshToken) return response } @UsePipes(new ValidationPipe()) @HttpCode(200) @Post('register') async register( @Body() dto: RegisterDto, @Res({ passthrough: true }) res: Response ) { const { refreshToken, ...response } = await this.authService.register(dto) this.authService.addRefreshTokenToResponse(res, refreshToken) return response } @HttpCode(200) @Post('login/access-token') async getNewTokens( @Req() req: Request, @Res({ passthrough: true }) res: Response ) { const refreshTokenFromCookies = req.cookies[this.authService.REFRESH_TOKEN_NAME] if (!refreshTokenFromCookies) { this.authService.removeRefreshTokenFromResponse(res) throw new UnauthorizedException('Refresh token not passed') } const { refreshToken, ...response } = await this.authService.getNewTokens( refreshTokenFromCookies ) this.authService.addRefreshTokenToResponse(res, refreshToken) return response } @HttpCode(200) @Post('logout') async logout(@Res({ passthrough: true }) res: Response) { this.authService.removeRefreshTokenFromResponse(res) return true } }