File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -5,18 +5,14 @@ import {
55 Logger ,
66 UnauthorizedException ,
77} from '@nestjs/common' ;
8- import { createRemoteJWKSet , jwtVerify } from 'jose' ;
98import * as crypto from 'crypto' ;
109
1110@Injectable ( )
1211export 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
You can’t perform that action at this time.
0 commit comments