Using Clerk's `ClerkExpressRequireAuth` in a NestJS guard

219 views Asked by At

I'm having trouble using the resolve(true) in a nestjs auth guard with Clerk's middleware implementation

@Injectable()
export class UserAuthGuard implements CanActivate {
  private readonly requireAuth = ClerkExpressRequireAuth({

  });
  canActivate(context: ExecutionContext): boolean | Promise<boolean> | Observable<boolean> {
    const request = context.switchToHttp().getRequest();
    const response = context.switchToHttp().getResponse();
    const next = context.switchToHttp().getNext();

    return new Promise((resolve, reject) => {
      this.requireAuth(request, response, (err) => {
        if (err) {
          reject(new UnauthorizedException(err.stack));
        } else {
          resolve(true)
        }
      });
    });
  }
}

If there is an error (invalid jwt token), the guard works well, returning the stack below

{
    "message": "Error: Unauthenticated\n    at UserAuthGuard.requireAuth (/@/node_modules/@clerk/clerk-sdk-node/src/clerkExpressRequireAuth.ts:60:12)",
    "error": "Unauthorized",
    "statusCode": 401
}

However, if correct token is passed, all i get is an empty body with a 401 code enter image description here

i have tried using resolve(true), and return true;, all i get is the empty response. Which configs am i missing

0

There are 0 answers