@@ -3,6 +3,7 @@ import { Injectable, NestMiddleware, UnauthorizedException } from '@nestjs/commo
3
3
import { AuthService } from '../auth.service' ;
4
4
import { FastifyReply , FastifyRequest } from 'fastify' ;
5
5
import * as ApiKey from 'uuid-apikey' ;
6
+ import { log } from 'readmeio' ;
6
7
7
8
@Injectable ( )
8
9
export class AuthMiddleware implements NestMiddleware {
@@ -11,15 +12,27 @@ export class AuthMiddleware implements NestMiddleware {
11
12
async use ( req : FastifyRequest [ 'raw' ] , res : FastifyReply [ 'raw' ] , next : ( ) => void ) {
12
13
const token = req ?. headers [ 'x-api-key' ] || req [ 'query' ] [ 'token' ] ;
13
14
14
- if ( ! token ) throw new UnauthorizedException ( 'В запросе не указан токен!' ) ;
15
+ if ( ! token ) {
16
+ log ( process . env . README_API_KEY , req , res , { apiKey : null , label : null } ) ;
17
+
18
+ throw new UnauthorizedException ( 'В запросе не указан токен!' ) ;
19
+ }
15
20
// @ts -ignore
16
- if ( ! ApiKey . isAPIKey ( token ) ) throw new UnauthorizedException ( 'Переданный токен некорректен!' ) ;
21
+ if ( ! ApiKey . isAPIKey ( token ) ) {
22
+ log ( process . env . README_API_KEY , req , res , { apiKey : null , label : null } ) ;
23
+
24
+ throw new UnauthorizedException ( 'Переданный токен некорректен!' ) ;
25
+ }
17
26
18
27
const user = await this . authService . findUserByToken ( token ) ;
28
+
19
29
const isLimitNotExceeded = await this . authService . checkAndDecreaseLimit ( token ) ;
20
30
31
+ log ( process . env . README_API_KEY , req , res , { apiKey : token , label : user . username || String ( user . userId ) } ) ;
32
+
21
33
req [ 'user' ] = user ;
22
34
req [ 'isLimitNotExceeded' ] = isLimitNotExceeded ;
35
+
23
36
next ( ) ;
24
37
}
25
38
}
0 commit comments