-
Notifications
You must be signed in to change notification settings - Fork 296
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Somente exibir campo de Contato quando logado(staff ou user) #77
Changes from all commits
9f5fabc
13d65ba
8db01be
ecdf8f4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { createParamDecorator, ExecutionContext } from '@nestjs/common'; | ||
|
||
export const UserDecorator = createParamDecorator( | ||
(data: unknown, ctx: ExecutionContext) => { | ||
const request = ctx.switchToHttp().getRequest(); | ||
return request?.user; | ||
}, | ||
); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
|
||
@Injectable() | ||
export class ApplyUser extends AuthGuard('jwt') { | ||
handleRequest(err: any, user: any) { | ||
if (user) return user; | ||
return null; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ import { ApiTags } from '@nestjs/swagger'; | |
import { ShelterService } from './shelter.service'; | ||
import { ServerResponse } from '../utils'; | ||
import { StaffGuard } from '@/guards/staff.guard'; | ||
import { ApplyUser } from '@/guards/apply-user.guard'; | ||
import { UserDecorator } from '@/decorators/UserDecorator/user.decorator'; | ||
|
||
@ApiTags('Abrigos') | ||
@Controller('shelters') | ||
|
@@ -35,9 +37,12 @@ export class ShelterController { | |
} | ||
|
||
@Get(':id') | ||
async show(@Param('id') id: string) { | ||
@UseGuards(ApplyUser) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. aqui eu acho que não precisa do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. aaah sim, te entendi. faz sentido. pergunta: o nest.js não deveria ter um "ApplyUser" nativo não? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. não sei/consegui achar, se souber como eu altero já também |
||
async show(@UserDecorator() user: any, @Param('id') id: string) { | ||
try { | ||
const data = await this.shelterService.show(id); | ||
const isLogged = | ||
Boolean(user) && Boolean(user?.sessionId) && Boolean(user?.userId); | ||
const data = await this.shelterService.show(id, isLogged); | ||
return new ServerResponse(200, 'Successfully get shelter', data); | ||
} catch (err: any) { | ||
this.logger.error(`Failed to get shelter: ${err}`); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
esses dois ifs aqui ficaram redundantes, não?
porque se não tiver sessionId nem userId, a query abaixo já vai retornar false de qualquer jeito
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
É só uma guard clause pra se não for informada nem gastar uma query com dado inválido. Posso remover se for o caso...