Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[SUPPORT] Cannot read property 'roles' of undefined #33

Closed
vishnu-dev opened this issue Apr 28, 2020 · 1 comment · Fixed by #34
Closed

[SUPPORT] Cannot read property 'roles' of undefined #33

vishnu-dev opened this issue Apr 28, 2020 · 1 comment · Fixed by #34

Comments

@vishnu-dev
Copy link

I have set up all the roles properly and have used decorators in my controller, but it throws an error. What am I doing wrong here, I followed the documentation.

Roles

appRoles
  .grant('superadmin')
  .createAny(['clients', 'users'])
  .readAny(['clients', 'users'])
  .updateAny(['clients', 'users'])
  .deleteAny(['clients', 'users'])

Controller

@UseGuards(JwtAuthGuard, ACGuard)
@UseRoles({
	resource:  'users',
	action:  'read',
	possession:  'any',
})
@Get()
allUsers(@UserRoles() userRoles: any) {
  console.log(userRoles);
  return this.userService.findAll();
}

req.user gives

{
  id: 'uuid',
  firstName: 'John',
  lastName: 'John',
  email: 'john@doe.com',
  createdAt: 2020-04-23T12:15:41.494Z,
  updatedAt: 2020-04-23T12:15:41.494Z,
  roles: [ 'superadmin' ]
}

The error

[Nest] 29258   - 04/28/2020, 12:11:35 PM   [ExceptionsHandler] Cannot read property 'roles' of undefined +14527ms
TypeError: Cannot read property 'roles' of undefined
    at /redacted/node_modules/nest-access-control/decorators/user-roles.decorators.js:11:45
    at /redacted/node_modules/@nestjs/core/helpers/context-utils.js:32:28
    at resolveParamValue (/redacted/node_modules/@nestjs/core/router/router-execution-context.js:139:31)
    at Array.map (<anonymous>)
    at pipesFn (/redacted/node_modules/@nestjs/core/router/router-execution-context.js:144:45)
    at /redacted/node_modules/@nestjs/core/router/router-execution-context.js:36:36
    at InterceptorsConsumer.intercept (/redacted/node_modules/@nestjs/core/interceptors/interceptors-consumer.js:10:20)
    at /redacted/node_modules/@nestjs/core/router/router-execution-context.js:45:60
    at processTicksAndRejections (internal/process/task_queues.js:93:5)
    at async /redacted/node_modules/@nestjs/core/router/router-proxy.js:8:17

@ruslanguns
Copy link
Contributor

That happens because ACGuard has the asumption that you are saving the roles in the user header. Make sure to make it accesible in the user object by req.user.roles

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants