You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I have a user role guard that needs to access a TypeORM repository inside. I set it inside my AppModule as a global guard. However, I see that when a GraphQL request comes, it does not go through the guard but simply hits my GraphQL query. Any idea what is going wrong here?
AppModule
@Module({imports: [GraphQLModule.forRoot<ApolloDriverConfig>({driver: ApolloDriver,autoSchemaFile: 'src/schema.gql',context: ({ req })=>({
req,user: newJwtService().decode((req.headers.authorizationasstring).replace('Bearer ',''),),}),}),// I need to make `TypeOrmModule` asynchronous because not all the requests// will need a database connection, and the requests that need will carry// the information to initialize the database connection inside the// authorization header as a JWT.//// e.g.: Check the following payload inside the JWT.//// ```json// {// "id": "2",// "database": "test",// "iat": 1696519539,// "exp": 1696526739// }// ```//TypeOrmModule.forRootAsync({useClass: TypeOrmConfigService,dataSourceFactory: async(options)=>awaitTypeOrmConfigService.getOrCreateDataSource(options),}),UserDetailsModule,],providers: [AppResolver,{provide: APP_GUARD,// I'm using the factory method here since the repository needs to change based// on the customer. We have a separate database for each customer. If I use// `useClass: UserRightGuard` syntax here, I get `undefined` for `reflector`.useFactory: (reflector,repo)=>newUserRightGuard(reflector,repo),inject: [Reflector,getRepositoryToken(UserDetails)],// If I make this request scoped, the guard will execute but that is not// the solution I want since not all the requests (e.g.: login) will have// user info in the request header.//// scope: Scope.REQUEST,},],})exportclassAppModule{}
UserRightGuard
@Injectable()exportclassUserRightGuardimplementsCanActivate{constructor(privatereflector: Reflector,privateuserDetailsRepo: Repository<UserDetails>,){}asynccanActivate(context: ExecutionContext): Promise<boolean>{try{constrequiredRights=this.reflector.getAllAndOverride(RequireRights,[context.getHandler(),context.getClass(),]);// Returns `true` if there is no right specified as required.if(!requiredRights)returntrue;constctx=GqlExecutionContext.create(context).getContext();const{ user }=ctx;const{ right }=awaitthis.userDetailsRepo.findOne({where: {id: user.id},});returnrequiredRights.some((r)=>r==right);}catch(err){returnfalse;}}}
I would expect the GraphQL queries annotated using @RequireRights(['subscription']) to invoke the guard and check if the user who made the request has appropriate right value in their user_details table. If the user does not have proper right, the query should return FORBIDDEN error inside its response body.
Is there an existing issue for this?
Current behavior
I have a user role guard that needs to access a TypeORM repository inside. I set it inside my AppModule as a global guard. However, I see that when a GraphQL request comes, it does not go through the guard but simply hits my GraphQL query. Any idea what is going wrong here?
AppModule
UserRightGuard
UserDetailsResolver
Minimum reproduction code
https://github.com/ErangaHeshan/nests-issues-trigger-global-guard#2-a-user-without-proper-access
Steps to reproduce
No response
Expected behavior
I would expect the GraphQL queries annotated using
@RequireRights(['subscription'])
to invoke the guard and check if the user who made the request has appropriateright
value in theiruser_details
table. If the user does not have properright
, the query should returnFORBIDDEN
error inside its response body.Package version
12.0.8
Graphql version
graphql
: 16.8.1@apollo/server
: 4.9.4@nestjs/platform-express
: 10.0.0@nestjs/typeorm
: 10.0.0NestJS version
10.0.0
Node.js version
16.13.2
In which operating systems have you tested?
Other
I discussed the issue with one of the NestJS core team member and here is our discussion on Discord
The text was updated successfully, but these errors were encountered: