Skip to content

Commit

Permalink
fix(middleware): enable setting entities in middleware classes
Browse files Browse the repository at this point in the history
- Remove commented-out lines to set the entity object correctly
- Update token, admin, and auth key middlewares for improved functionality

(Your middleware logic is so convoluted, even Rubik's Cube would give up)
  • Loading branch information
tomer-shvadron committed Jan 1, 2025
1 parent bfa3a23 commit 503b300
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
2 changes: 1 addition & 1 deletion services/workflows-service/prisma/data-migrations
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ export class TokenAuthGuard implements CanActivate {
throw new UnauthorizedException('Token has expired');
}

// this.cls.set('entity', {
// endUser: {
// workflowRuntimeDataId: tokenEntity.workflowRuntimeDataId,
// endUserId: tokenEntity.endUserId,
// id: tokenEntity.id,
// },
// type: 'endUser',
// });
this.cls.set('entity', {
endUser: {
workflowRuntimeDataId: tokenEntity.workflowRuntimeDataId,
endUserId: tokenEntity.endUserId,
id: tokenEntity.id,
},
type: 'endUser',
});

(req as any).tokenScope = tokenEntity;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export class AdminKeyMiddleware implements NestMiddleware {
type: 'admin',
};

// this.cls.set('entity', entity);
this.cls.set('entity', entity);

// @ts-expect-error
req.user = entity;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ export class AuthKeyMiddleware implements NestMiddleware {
const { id, name, projects, authenticationConfiguration, ...customerWithoutProjects } =
dbApiKey.customer;

// this.cls.set('entity', {
// customer: {
// id,
// name,
// },
// type: 'customer',
// });
this.cls.set('entity', {
customer: {
id,
name,
},
type: 'customer',
});

req.user = {
customer: customerWithoutProjects,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export class RequestIdMiddleware implements NestMiddleware {
private setReqId(req: Request<unknown>, res: Response<unknown>) {
try {
req.id = randomUUID();
// this.cls.set('requestId', req.id);
this.cls.set('requestId', req.id);
res.setHeader('X-Request-ID', req.id);
} catch (e) {
// Mainly for debugging purposes. See https://github.com/Papooch/nestjs-cls/issues/67
Expand Down

0 comments on commit 503b300

Please sign in to comment.