Skip to content

Commit 7cc3b16

Browse files
committed
fix: switch to dynamic import for jose to resolve esm/cjs conflict on vercel
1 parent 1101101 commit 7cc3b16

1 file changed

Lines changed: 10 additions & 6 deletions

File tree

src/auth/auth.guard.ts

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,14 @@ import {
55
Logger,
66
UnauthorizedException,
77
} from '@nestjs/common';
8-
import { createRemoteJWKSet, jwtVerify } from 'jose';
98
import * as crypto from 'crypto';
109

1110
@Injectable()
1211
export class AuthGuard implements CanActivate {
1312
private readonly logger = new Logger(AuthGuard.name);
1413
private JWKS: any;
1514

16-
constructor() {
17-
const authUrl = process.env.AUTH_SERVICE_URL || 'https://auth.openlake.in';
18-
this.JWKS = createRemoteJWKSet(new URL(`${authUrl}/.well-known/jwks.json`));
19-
}
15+
constructor() {}
2016

2117
async canActivate(context: ExecutionContext): Promise<boolean> {
2218
const request = context.switchToHttp().getRequest();
@@ -27,7 +23,15 @@ export class AuthGuard implements CanActivate {
2723
}
2824

2925
try {
30-
const { payload } = await jwtVerify(token, this.JWKS, {
26+
// Dynamic import for ESM compatibility in Vercel/CommonJS environment
27+
const jose = await import('jose');
28+
29+
if (!this.JWKS) {
30+
const authUrl = process.env.AUTH_SERVICE_URL || 'https://auth.openlake.in';
31+
this.JWKS = jose.createRemoteJWKSet(new URL(`${authUrl}/.well-known/jwks.json`));
32+
}
33+
34+
const { payload } = await jose.jwtVerify(token, this.JWKS, {
3135
issuer: process.env.AUTH_ISSUER || 'openlake-auth',
3236
});
3337

0 commit comments

Comments
 (0)