@@ -4,6 +4,7 @@ import { InjectRepository } from '@nestjs/typeorm';
4
4
import { HeaderAPIKeyStrategy } from 'passport-headerapikey' ;
5
5
import { Repository } from 'typeorm' ;
6
6
import { ApiKey } from '../entities/users/api-key.entity' ;
7
+ import { ApiUsageService } from '../services/api-usage.service' ;
7
8
8
9
@Injectable ( )
9
10
export class ApiKeyStrategy extends PassportStrategy (
@@ -13,10 +14,9 @@ export class ApiKeyStrategy extends PassportStrategy(
13
14
constructor (
14
15
@InjectRepository ( ApiKey )
15
16
private apiKeyRepository : Repository < ApiKey > ,
17
+ private apiUsageService : ApiUsageService ,
16
18
) {
17
19
super ( { header : 'X-API-KEY' , prefix : '' } , true , async ( apiKey , done ) => {
18
- console . log ( 'apiKey' , apiKey ) ;
19
- console . log ( 'done' , done ) ;
20
20
return this . validate ( apiKey , done ) ;
21
21
} ) ;
22
22
}
@@ -25,10 +25,7 @@ export class ApiKeyStrategy extends PassportStrategy(
25
25
apiKey : string ,
26
26
done : ( error : Error | null , data : ApiKey | boolean ) => void ,
27
27
) {
28
- console . log ( 'Received API Key:' , apiKey ) ;
29
-
30
28
if ( ! apiKey ) {
31
- console . log ( 'No API Key provided' ) ;
32
29
return done ( new UnauthorizedException ( 'Missing API Key' ) , false ) ;
33
30
}
34
31
@@ -37,16 +34,15 @@ export class ApiKeyStrategy extends PassportStrategy(
37
34
where : { id : apiKey } ,
38
35
} ) ;
39
36
40
- console . log ( 'Database query result:' , key ) ;
41
-
42
37
if ( ! key || key . total_requests_remaining <= 0 ) {
43
- console . log ( 'Invalid or expired API Key' ) ;
44
38
return done ( new UnauthorizedException ( 'Invalid API Key' ) , false ) ;
45
39
}
46
40
41
+ // Track API usage after successful validation
42
+ await this . apiUsageService . trackUsage ( apiKey ) ;
43
+
47
44
return done ( null , key ) ;
48
45
} catch ( error ) {
49
- console . log ( 'Error during validation:' , error ) ;
50
46
return done ( new UnauthorizedException ( 'Invalid API Key' ) , false ) ;
51
47
}
52
48
}
0 commit comments