87 lines
2.0 KiB
TypeScript
87 lines
2.0 KiB
TypeScript
import {
|
|
Body,
|
|
Param,
|
|
Controller,
|
|
Get,
|
|
HttpCode,
|
|
HttpStatus,
|
|
Post,
|
|
Put,
|
|
Request,
|
|
UseGuards,
|
|
Delete,
|
|
ParseIntPipe,
|
|
Query
|
|
} 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) {
|
|
const { rememberMe } = data;
|
|
return this.authService.signIn(data, rememberMe);
|
|
}
|
|
|
|
//@UseGuards()
|
|
@Post("register")
|
|
//@Roles(Role.Admin)
|
|
async register(@Body() data: registerDto){
|
|
return this.authService.register(data)
|
|
}
|
|
|
|
//@UseGuards(AuthGuard)
|
|
@Get()
|
|
findAll() {
|
|
return this.authService.findAll();
|
|
}
|
|
|
|
@Get('page')
|
|
findAllPaginated(
|
|
@Query('page') page: number = 1,
|
|
@Query('limit') limit: number = 10,
|
|
@Query() filters: any
|
|
) {
|
|
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) {
|
|
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) {
|
|
return this.authService.update(id_usuario, data);
|
|
}
|
|
|
|
//@UseGuards(AuthGuard)
|
|
@Delete(':id_usuario')
|
|
@Roles(Role.Admin)
|
|
async remove(@Param('id_usuario') id_usuario: number) {
|
|
await this.authService.remove(id_usuario);
|
|
return { message: 'User successfully deleted' };
|
|
}
|
|
|
|
@HttpCode(HttpStatus.OK)
|
|
@Post('validate')
|
|
async validateToken(@Body('token') token: string) {
|
|
return this.authService.validateToken(token);
|
|
}
|
|
|
|
} |