-
Notifications
You must be signed in to change notification settings - Fork 0
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
Excalibur
authored and
Excalibur
committed
Sep 10, 2022
1 parent
d6c9e53
commit eb909f7
Showing
30 changed files
with
274 additions
and
65 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
{ | ||
"version": "0.2.0", | ||
"configurations": [ | ||
{ | ||
"type": "node", | ||
"request": "launch", | ||
"name": "Debug Nest Framework", | ||
"args": ["${workspaceFolder}/src/main.ts"], | ||
"runtimeArgs": [ | ||
"--nolazy", | ||
"-r", | ||
"ts-node/register", | ||
"-r", | ||
"tsconfig-paths/register" | ||
], | ||
"sourceMaps": true, | ||
"envFile": "${workspaceFolder}/.env", | ||
"cwd": "${workspaceRoot}", | ||
"console": "integratedTerminal", | ||
"autoAttachChildProcesses": 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
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,13 @@ | ||
import { applyDecorators, UseGuards } from '@nestjs/common'; | ||
import { AuthGuard } from '@nestjs/passport'; | ||
|
||
import { RoleProtected } from 'src/auth/decorators'; | ||
import { ValidRoles } from 'src/shared/enums'; | ||
import { UserRoleGuard } from '../guards/user-role.guard'; | ||
|
||
export function Auth(...roles: ValidRoles[]) { | ||
return applyDecorators( | ||
RoleProtected(...roles), | ||
UseGuards(AuthGuard(), UserRoleGuard), | ||
); | ||
} |
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,19 @@ | ||
import { | ||
createParamDecorator, | ||
ExecutionContext, | ||
InternalServerErrorException, | ||
} from '@nestjs/common'; | ||
|
||
import { MessageHandler } from 'src/shared/enums'; | ||
|
||
export const GetUser = createParamDecorator( | ||
(data: string, ctx: ExecutionContext) => { | ||
const req = ctx.switchToHttp().getRequest(); | ||
const user = req.user; | ||
|
||
if (!user) | ||
throw new InternalServerErrorException(MessageHandler.USER_NOT_FOUND); | ||
|
||
return !data ? user : user[data]; | ||
}, | ||
); |
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,3 @@ | ||
export { Auth } from './auth.decorator'; | ||
export { RoleProtected } from './role-protected.decorator'; | ||
export { GetUser } from './get-user.decorator'; |
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,5 @@ | ||
import { SetMetadata } from '@nestjs/common'; | ||
import { MetaRoles, ValidRoles } from 'src/shared/enums'; | ||
|
||
export const RoleProtected = (...args: ValidRoles[]) => | ||
SetMetadata(MetaRoles.ROLES, args); |
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,41 @@ | ||
import { Reflector } from '@nestjs/core'; | ||
import { | ||
BadRequestException, | ||
CanActivate, | ||
ExecutionContext, | ||
ForbiddenException, | ||
Injectable, | ||
} from '@nestjs/common'; | ||
import { Observable } from 'rxjs'; | ||
|
||
import { MessageHandler, MetaRoles } from 'src/shared/enums'; | ||
import { User } from 'src/user/entities/user.entity'; | ||
|
||
@Injectable() | ||
export class UserRoleGuard implements CanActivate { | ||
constructor(private readonly reflector: Reflector) {} | ||
canActivate( | ||
context: ExecutionContext, | ||
): boolean | Promise<boolean> | Observable<boolean> { | ||
const validRoles: string[] = this.reflector.get( | ||
MetaRoles.ROLES, | ||
context.getHandler(), | ||
); | ||
|
||
if (!validRoles) return true; | ||
if (validRoles.length === 0) return true; | ||
|
||
const req = context.switchToHttp().getRequest(); | ||
const user = req.user as User; | ||
|
||
if (!user) throw new BadRequestException(MessageHandler.USER_NOT_FOUND); | ||
|
||
for (const role of user.roles) { | ||
if (validRoles.includes(role)) { | ||
return true; | ||
} | ||
} | ||
|
||
throw new ForbiddenException(MessageHandler.USER_INVALID_ROLE); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,3 +1,3 @@ | ||
export interface JwtPayload { | ||
email: string; | ||
id: string; | ||
} |
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 |
---|---|---|
@@ -1,13 +1,15 @@ | ||
import { Module } from '@nestjs/common'; | ||
import { TypeOrmModule } from '@nestjs/typeorm'; | ||
|
||
import { AuthModule } from 'src/auth/auth.module'; | ||
|
||
import { ProductsService } from './products.service'; | ||
import { ProductsController } from './products.controller'; | ||
import { Product } from './entities/product.entity'; | ||
|
||
@Module({ | ||
controllers: [ProductsController], | ||
providers: [ProductsService], | ||
imports: [TypeOrmModule.forFeature([Product])], | ||
imports: [TypeOrmModule.forFeature([Product]), AuthModule], | ||
}) | ||
export class ProductsModule {} |
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
File renamed without changes.
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 @@ | ||
export { RawHeaders } from './raw-headers.decorator'; |
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,11 @@ | ||
import { createParamDecorator, ExecutionContext } from '@nestjs/common'; | ||
|
||
export const RawHeaders = createParamDecorator( | ||
(data: string, ctx: ExecutionContext) => { | ||
const req = ctx.switchToHttp().getRequest(); | ||
return req.rawHeaders; | ||
}, | ||
); | ||
|
||
//uso @RawHeader() rawHeaders: string[] | ||
// best use @Headers() headers: IncomingHttpHeaders |
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 @@ | ||
export { MessageHandler } from './message.handler'; | ||
export { MetaRoles } from './meta.roles'; | ||
export { ValidRoles } from './valid.roles'; | ||
export { ValidState } from './valid.state'; |
10 changes: 10 additions & 0 deletions
10
src/utils/enums/message.handler.ts → src/shared/enums/message.handler.ts
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 |
---|---|---|
@@ -1,7 +1,17 @@ | ||
export enum MessageHandler { | ||
PASSWORD_INVALID = 'The password must have a Uppercase, lowercase letter and a number', | ||
|
||
UNEXPECTED_ERROR = 'Unexpected error, check server logs', | ||
|
||
UNAUTHORIZED_CREDENTIALS = 'Email or password are not valid', | ||
UNAUTHORIZED_TOKEN = 'Token is not valid', | ||
UNAUTHORIZED_USER = 'User is inactive, contact support', | ||
|
||
USERS_NOT_FOUND = 'Users not found', | ||
USER_NOT_FOUND = 'User not found', | ||
USER_INVALID_ROLE = 'User does not have permissions', | ||
USER_INVALID_STATUS = 'User does not have valid status', | ||
USER_INACTIVE = 'User is inactive', | ||
|
||
EMAIL_ALREADY_EXIST = 'Email is all ready exist', | ||
} |
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,3 @@ | ||
export enum MetaRoles { | ||
ROLES = 'roles', | ||
} |
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 @@ | ||
export enum ValidRoles { | ||
USER = 'User', | ||
ADMIN = 'Admin', | ||
} |
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,6 @@ | ||
export enum ValidState { | ||
ACTIVE = 'Active', | ||
PREREGISTER = 'Preregister', | ||
INACTIVE = 'Inactive', | ||
SUSPENDED = 'Suspended', | ||
} |
Oops, something went wrong.