admin管理员组文章数量:1308118
I'm new to nestJs and I needed to add role based access to the application so I followed the documentation but in the execution context user doesn't exist. I can't seems to find the problem here's the github repo if you need to seem more code:
roles.guard.ts
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/mon';
import { Reflector } from '@nestjs/core';
import { ROLES_KEY } from 'src/decorators/roles.decorator';
import Role from 'src/util/enums/role.enum';
@Injectable()
export class RolesGuard implements CanActivate {
constructor(private reflector: Reflector) {}
canActivate(context: ExecutionContext): boolean {
const requiredRoles = this.reflector.getAllAndOverride<Role[]>(ROLES_KEY, [
context.getHandler(),
context.getClass(),
]);
if (!requiredRoles) {
return true;
}
const { user } = context.switchToHttp().getRequest();
console.log(context.switchToHttp().getRequest().req);
return requiredRoles.some((role) => user.type === role);
}
}
app.controller.ts
@UseGuards(JwtAuthGuard, RolesGuard)
@Get('me/business')
@Roles(Role.ADMIN)
getBusiness(@Request() req) {
return this.usersService.getUserBusiness(req.user.id);
}
I'm new to nestJs and I needed to add role based access to the application so I followed the documentation but in the execution context user doesn't exist. I can't seems to find the problem here's the github repo if you need to seem more code: https://github./anjula-sack/slinc-backend
roles.guard.ts
import { Injectable, CanActivate, ExecutionContext } from '@nestjs/mon';
import { Reflector } from '@nestjs/core';
import { ROLES_KEY } from 'src/decorators/roles.decorator';
import Role from 'src/util/enums/role.enum';
@Injectable()
export class RolesGuard implements CanActivate {
constructor(private reflector: Reflector) {}
canActivate(context: ExecutionContext): boolean {
const requiredRoles = this.reflector.getAllAndOverride<Role[]>(ROLES_KEY, [
context.getHandler(),
context.getClass(),
]);
if (!requiredRoles) {
return true;
}
const { user } = context.switchToHttp().getRequest();
console.log(context.switchToHttp().getRequest().req);
return requiredRoles.some((role) => user.type === role);
}
}
app.controller.ts
@UseGuards(JwtAuthGuard, RolesGuard)
@Get('me/business')
@Roles(Role.ADMIN)
getBusiness(@Request() req) {
return this.usersService.getUserBusiness(req.user.id);
}
Share
Improve this question
asked May 9, 2021 at 3:09
Anjula SamarasingheAnjula Samarasinghe
2371 gold badge5 silver badges13 bronze badges
4
-
Do you have the
RolesGuard
bound globally by chance? – Jay McDoniel Commented May 9, 2021 at 3:22 - yeah it's in the app.module.ts – Anjula Samarasinghe Commented May 9, 2021 at 3:31
-
Could you share the documentation you followed? Also, could you explain how you send the
user
in the request? Is it inside the header or the body? I tried the github URL you posted, but I guess it is a private repository so I can't see – Eranga Heshan Commented May 9, 2021 at 4:52 - can you check now? I made it public @ErangaHeshan – Anjula Samarasinghe Commented May 9, 2021 at 5:00
1 Answer
Reset to default 7From the code, I think you are mixing global and local guard
In app.module.ts
, the below code is for registering global guard.
and app.useGlobalGuard()
should be used together if you want to apply guard globally.
// Remove the following code in app.module.ts
{
provide: APP_GUARD,
useClass: RolesGuard,
}
But your intention should be building a local role guard, so please remove the above code and the request user will work.
本文标签: javascriptUser is undefined on the contextswitchToHttp()getRequest() nestjsStack Overflow
版权声明:本文标题:javascript - User is undefined on the context.switchToHttp().getRequest() nestjs - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741855691a2401326.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论