101 lines
2.4 KiB
TypeScript
101 lines
2.4 KiB
TypeScript
import {
|
|
Body,
|
|
Param,
|
|
Controller,
|
|
Get,
|
|
HttpCode,
|
|
HttpStatus,
|
|
Post,
|
|
Put,
|
|
Request,
|
|
UseGuards,
|
|
Delete,
|
|
ParseIntPipe,
|
|
Query,
|
|
Logger,
|
|
Req,
|
|
} from '@nestjs/common';
|
|
import { AuthGuard } from './auth.guard';
|
|
import { AuthService } from './auth.service';
|
|
import { registerDto } from './dto/registerDto.dto';
|
|
import { Roles } from '../permissions/roles.decorator';
|
|
import { RolesGuard } from '../permissions/roles.guard';
|
|
import { Role } from '../permissions/role.enum';
|
|
|
|
@Controller('auth')
|
|
@UseGuards(RolesGuard)
|
|
export class AuthController {
|
|
constructor(private authService: AuthService) {}
|
|
|
|
@HttpCode(HttpStatus.OK)
|
|
@Post('login')
|
|
async signIn(@Body() data: registerDto) {
|
|
Logger.debug('signIn');
|
|
const { rememberMe } = data;
|
|
return this.authService.signIn(data, rememberMe);
|
|
}
|
|
|
|
//@UseGuards()
|
|
@Post('register')
|
|
@Roles(Role.Admin)
|
|
async register(@Body() data: registerDto) {
|
|
Logger.debug('register user');
|
|
return this.authService.register(data);
|
|
}
|
|
|
|
@Get()
|
|
findAll() {
|
|
Logger.debug('find all users');
|
|
return this.authService.findAll();
|
|
}
|
|
|
|
@Get('page')
|
|
findAllPaginated(
|
|
@Query('page') page: number = 1,
|
|
@Query('limit') limit: number = 10,
|
|
@Query() filters: any,
|
|
) {
|
|
Logger.debug('find all users max 10');
|
|
page = page < 1 ? 1 : page;
|
|
limit = limit > 10 || limit < 1 ? 10 : limit;
|
|
|
|
return this.authService.findAllPaginated(page, limit, filters);
|
|
}
|
|
|
|
@Get(':id_usuario')
|
|
async profile(@Param('id_usuario', ParseIntPipe) id_usuario: number) {
|
|
Logger.debug('find one user');
|
|
return this.authService.profile(id_usuario);
|
|
}
|
|
|
|
//@UseGuards(AuthGuard)
|
|
@Put(':id_usuario')
|
|
@Roles(Role.Admin)
|
|
async update(
|
|
@Param('id_usuario') id_usuario: number,
|
|
@Body() data: registerDto,
|
|
@Req() req: any
|
|
) {
|
|
const authHeader = req.headers['authorization'];
|
|
const [, token] = authHeader.split(' ');
|
|
|
|
return this.authService.update(id_usuario, data, token);
|
|
}
|
|
|
|
//@UseGuards(AuthGuard)
|
|
@Delete(':id_usuario')
|
|
@Roles(Role.Admin)
|
|
async remove(@Param('id_usuario') id_usuario: number) {
|
|
Logger.debug('delete user');
|
|
await this.authService.remove(id_usuario);
|
|
return { message: 'User successfully deleted' };
|
|
}
|
|
|
|
@HttpCode(HttpStatus.OK)
|
|
@Post('validate')
|
|
async validateToken(@Body('token') token: string) {
|
|
Logger.debug('validate user');
|
|
return this.authService.validateToken(token);
|
|
}
|
|
}
|