Skip to content

Commit

Permalink
fix: user decorator context improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
ruslanguns committed May 10, 2020
1 parent a74dfb9 commit 0ef7de2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 5 deletions.
5 changes: 3 additions & 2 deletions lib/decorators/user-roles.decorators.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const common_1 = require("@nestjs/common");
* You can pass an optional property key to the decorator to get it from the user object
* e.g `@UserRoles('premissions')` will return the `req.user.premissions` instead.
*/
exports.UserRoles = common_1.createParamDecorator((data, req) => {
return data ? req.user[data] : req.user.roles;
exports.UserRoles = common_1.createParamDecorator((data, ctx) => {
const request = ctx.switchToHttp().getRequest();
return data ? request.user[data] : request.user.roles;
});
8 changes: 5 additions & 3 deletions src/decorators/user-roles.decorators.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { createParamDecorator, PipeTransform } from '@nestjs/common';
import { createParamDecorator, ExecutionContext } from '@nestjs/common';

/**
* Access the user roles from the request object i.e `req.user.roles`.
*
* You can pass an optional property key to the decorator to get it from the user object
* e.g `@UserRoles('premissions')` will return the `req.user.premissions` instead.
*/
export const UserRoles = createParamDecorator((data: string, req) => {
return data ? req.user[data] : req.user.roles;
export const UserRoles = createParamDecorator((data: string, ctx: ExecutionContext) => {

const request = ctx.switchToHttp().getRequest();
return data ? request.user[data] : request.user.roles;
});

0 comments on commit 0ef7de2

Please sign in to comment.