Skip to content

Commit

Permalink
Merge pull request nestjsx#71 from tupe12334/feat/user-roles-not-crash
Browse files Browse the repository at this point in the history
  • Loading branch information
Shady Khalifa authored Feb 20, 2022
2 parents 6a24cc8 + 5b0b5b2 commit 62b2799
Showing 1 changed file with 10 additions and 6 deletions.
16 changes: 10 additions & 6 deletions src/decorators/user-roles.decorators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ function userFactory<T>(ctx: ExecutionContext): T {
*
* You can pass an optional property key to the decorator to get it from the user object
* e.g `@UserRoles('permissions')` will return the `req.user.permissions` instead.
* In case that the request is missing User object the function will return null
*/
export const UserRoles = createParamDecorator<string, ExecutionContext, (Role | string)[]>(
(data: string, ctx: ExecutionContext) => {
const user = userFactory<any>(ctx);
return data ? user[data] : user.roles;
},
);
export const UserRoles = createParamDecorator<
string | undefined,
ExecutionContext,
(Role | string)[] | null
>((propertyKey: undefined | string, ctx: ExecutionContext) => {
const user = userFactory<any>(ctx);
if (!user) return null;
return propertyKey ? user[propertyKey] : user.roles;
});

0 comments on commit 62b2799

Please sign in to comment.