generated from HenriqueAmorim20/GEROcuidadoAPITemplate
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d43bb5f
commit 1b5acd5
Showing
7 changed files
with
86 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { | ||
CanActivate, | ||
ExecutionContext, | ||
Inject, | ||
Injectable, | ||
UnauthorizedException, | ||
} from '@nestjs/common'; | ||
import { Reflector } from '@nestjs/core'; | ||
import { ClientProxy } from '@nestjs/microservices'; | ||
import { lastValueFrom, timeout } from 'rxjs'; | ||
import { IS_PUBLIC_KEY } from './shared/decorators/public-route.decorator'; | ||
|
||
@Injectable() | ||
export class AutenticacaoGuard implements CanActivate { | ||
constructor( | ||
@Inject('AUTH_CLIENT') | ||
private readonly _client: ClientProxy, | ||
private readonly _reflector: Reflector, | ||
) {} | ||
|
||
async canActivate(context: ExecutionContext) { | ||
const req = context.switchToHttp().getRequest(); | ||
const jwt = req.headers['authorization']?.split(' ')[1]; | ||
|
||
const isPublic = this._reflector.getAllAndOverride<boolean>(IS_PUBLIC_KEY, [ | ||
context.getHandler(), | ||
context.getClass(), | ||
]); | ||
|
||
if (isPublic) return true; | ||
|
||
const request = this._client | ||
.send({ role: 'auth', cmd: 'check' }, { jwt }) | ||
.pipe(timeout(5000)); | ||
const response = await lastValueFrom(request); | ||
|
||
if (!response) { | ||
throw new UnauthorizedException('Usuário não autenticado!'); | ||
} | ||
|
||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { SetMetadata } from '@nestjs/common'; | ||
|
||
export const IS_PUBLIC_KEY = 'isPublic'; | ||
export const PublicRoute = () => SetMetadata(IS_PUBLIC_KEY, true); |