Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/auth/decorators/guard.alex.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { applyDecorators, UseGuards } from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { ApiBearerAuth } from '@nestjs/swagger';
import { UserIsMe } from '../guards/alexR.guard';

export function AuthAlex() {
return applyDecorators(
//UseGuards(AuthGuard(), UserIsMe),
UseGuards(UserIsMe),
ApiBearerAuth(),
);
}
18 changes: 18 additions & 0 deletions src/auth/guards/alexR.guard.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import { CanActivate, ExecutionContext, ForbiddenException, Injectable } from '@nestjs/common';
import { Reflector } from '@nestjs/core';
import { Observable } from 'rxjs';

@Injectable()
export class UserIsMe implements CanActivate {
constructor(private readonly reflector: Reflector) {}

canActivate( context: ExecutionContext ): boolean | Promise<boolean> | Observable<boolean> {

const request = context.switchToHttp().getRequest() as Request

if (request.headers['id_user'] === 'a357dd17-136f-4556-ac45-5200b4f9ac55') return true

return false
}
}

8 changes: 8 additions & 0 deletions src/books/books.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import { Auth } from 'src/auth/decorators/auth.decorator';
import { ValidRoles } from 'src/auth/interfaces/valid-roles';
import { FilesInterceptor } from '@nestjs/platform-express';
import { UploadFilesDto } from './dto/upload-files.dto';
import { AuthAlex } from 'src/auth/decorators/guard.alex';

@ApiTags('Books')
@Controller('books')
Expand Down Expand Up @@ -123,4 +124,11 @@ export class BooksController {
remove(@Param('id', ParseUUIDPipe) id: string, @Request() req) {
return this.booksService.remove(id, req.user);
}

@AuthAlex()
@Get('coders/a357dd17-136f-4556-ac45-5200b4f9ac55')
codersAlex() {
return this.booksService.codersAlex();
}

}
11 changes: 11 additions & 0 deletions src/books/books.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -167,4 +167,15 @@ export class BooksService {
throw new InternalServerErrorException(error);
}
}

async codersAlex() {
const message = {
phrase: "Hello World!",
author: "Alexander Rodriguez"
}

return message;

}

}