saveinmed/backoffice/src/auth/auth.controller.ts

35 lines
1.2 KiB
TypeScript

import { Body, Controller, Get, HttpCode, HttpStatus, Post, Req, Res, UseGuards } from '@nestjs/common';
import { ApiBearerAuth, ApiTags } from '@nestjs/swagger';
import { FastifyReply } from 'fastify';
import { JwtAuthGuard } from './guards/jwt-auth.guard';
import { RefreshTokenGuard } from './guards/refresh-token.guard';
import { AuthService } from './auth.service';
import { LoginDto } from './dto/login.dto';
@ApiTags('auth')
@Controller('auth')
export class AuthController {
constructor(private readonly authService: AuthService) { }
@Post('login')
@HttpCode(HttpStatus.OK)
async login(@Body() dto: LoginDto, @Res({ passthrough: true }) reply: FastifyReply) {
return this.authService.login(dto, reply);
}
@Post('refresh')
@UseGuards(RefreshTokenGuard)
@HttpCode(HttpStatus.OK)
async refresh(@Req() req: any, @Res({ passthrough: true }) reply: FastifyReply) {
const userId = req.user?.sub;
const refreshToken = req.user?.refreshToken;
return this.authService.refreshTokens(userId, refreshToken, reply);
}
@Post('logout')
@UseGuards(JwtAuthGuard)
@ApiBearerAuth()
async logout(@Req() req: any, @Res({ passthrough: true }) reply: FastifyReply) {
return this.authService.logout(req.user.sub, reply);
}
}