Files
api_directorio/src/auth/auth.controller.ts
T

101 lines
2.4 KiB
TypeScript
Raw Normal View History

2024-06-06 20:32:46 -06:00
import {
Body,
2024-06-07 13:46:18 -06:00
Param,
2024-06-06 20:32:46 -06:00
Controller,
Get,
HttpCode,
HttpStatus,
Post,
2024-06-07 13:46:18 -06:00
Put,
2024-06-06 20:32:46 -06:00
Request,
2024-06-07 13:46:18 -06:00
UseGuards,
2024-06-26 13:31:33 -06:00
Delete,
2024-07-03 14:55:06 -06:00
ParseIntPipe,
2024-07-29 20:21:38 -06:00
Query,
Logger,
2024-08-18 23:03:28 -06:00
Req,
2024-06-06 20:32:46 -06:00
} from '@nestjs/common';
import { AuthGuard } from './auth.guard';
2024-06-05 14:35:05 -06:00
import { AuthService } from './auth.service';
2024-06-11 19:58:36 -06:00
import { registerDto } from './dto/registerDto.dto';
2024-07-29 20:21:38 -06:00
import { Roles } from '../permissions/roles.decorator';
import { RolesGuard } from '../permissions/roles.guard';
import { Role } from '../permissions/role.enum';
2024-06-05 14:35:05 -06:00
@Controller('auth')
2024-07-05 08:58:20 -06:00
@UseGuards(RolesGuard)
2024-06-05 14:35:05 -06:00
export class AuthController {
constructor(private authService: AuthService) {}
@HttpCode(HttpStatus.OK)
@Post('login')
2024-06-12 11:40:20 -06:00
async signIn(@Body() data: registerDto) {
2024-07-29 20:21:38 -06:00
Logger.debug('signIn');
2024-07-17 13:50:33 -06:00
const { rememberMe } = data;
return this.authService.signIn(data, rememberMe);
2024-06-05 14:35:05 -06:00
}
2024-06-24 16:12:23 -06:00
2024-06-24 17:02:02 -06:00
//@UseGuards()
2024-07-29 20:21:38 -06:00
@Post('register')
2024-08-18 23:03:28 -06:00
@Roles(Role.Admin)
2024-07-29 20:21:38 -06:00
async register(@Body() data: registerDto) {
Logger.debug('register user');
return this.authService.register(data);
2024-06-12 11:40:20 -06:00
}
2024-06-26 13:31:33 -06:00
@Get()
findAll() {
2024-07-29 20:21:38 -06:00
Logger.debug('find all users');
2024-06-26 13:31:33 -06:00
return this.authService.findAll();
}
2024-07-03 14:55:06 -06:00
@Get('page')
findAllPaginated(
@Query('page') page: number = 1,
@Query('limit') limit: number = 10,
2024-07-29 20:21:38 -06:00
@Query() filters: any,
2024-07-03 14:55:06 -06:00
) {
2024-07-29 20:21:38 -06:00
Logger.debug('find all users max 10');
2024-07-03 14:55:06 -06:00
page = page < 1 ? 1 : page;
limit = limit > 10 || limit < 1 ? 10 : limit;
2024-07-29 20:21:38 -06:00
return this.authService.findAllPaginated(page, limit, filters);
2024-07-03 14:55:06 -06:00
}
@Get(':id_usuario')
async profile(@Param('id_usuario', ParseIntPipe) id_usuario: number) {
2024-07-29 20:21:38 -06:00
Logger.debug('find one user');
2024-07-03 14:55:06 -06:00
return this.authService.profile(id_usuario);
2024-06-06 20:32:46 -06:00
}
2024-06-07 13:46:18 -06:00
2024-06-24 17:02:02 -06:00
//@UseGuards(AuthGuard)
2024-07-05 08:58:20 -06:00
@Put(':id_usuario')
2024-06-30 17:04:42 -06:00
@Roles(Role.Admin)
2024-07-29 20:21:38 -06:00
async update(
@Param('id_usuario') id_usuario: number,
@Body() data: registerDto,
2024-08-18 23:03:28 -06:00
@Req() req: any
2024-07-29 20:21:38 -06:00
) {
2024-08-18 23:03:28 -06:00
const authHeader = req.headers['authorization'];
const [, token] = authHeader.split(' ');
return this.authService.update(id_usuario, data, token);
2024-06-07 13:46:18 -06:00
}
2024-06-24 17:02:02 -06:00
//@UseGuards(AuthGuard)
2024-07-05 08:58:20 -06:00
@Delete(':id_usuario')
2024-06-30 17:04:42 -06:00
@Roles(Role.Admin)
2024-07-05 08:58:20 -06:00
async remove(@Param('id_usuario') id_usuario: number) {
2024-07-29 20:21:38 -06:00
Logger.debug('delete user');
2024-07-05 08:58:20 -06:00
await this.authService.remove(id_usuario);
2024-06-07 13:46:18 -06:00
return { message: 'User successfully deleted' };
}
2024-06-12 11:40:20 -06:00
@HttpCode(HttpStatus.OK)
@Post('validate')
async validateToken(@Body('token') token: string) {
2024-07-29 20:21:38 -06:00
Logger.debug('validate user');
2024-06-12 11:40:20 -06:00
return this.authService.validateToken(token);
}
2024-07-29 20:21:38 -06:00
}