-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: user decorator context improvements
See more details in: https://docs.nestjs.com/migration-guide#custom-route-decorators fix #33 and fix #32
- Loading branch information
1 parent
a74dfb9
commit 0ef7de2
Showing
2 changed files
with
8 additions
and
5 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
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; | ||
}); |