Skip to content
This repository has been archived by the owner on Apr 19, 2023. It is now read-only.

Commit

Permalink
♻️ Support logging authenticated requests
Browse files Browse the repository at this point in the history
  • Loading branch information
AnandChowdhary committed Nov 13, 2020
1 parent 841ffef commit f50a401
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 3 deletions.
2 changes: 1 addition & 1 deletion src/config/configuration.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export interface Configuration {
};

tracking: {
mode: 'all' | 'api-key';
mode: 'all' | 'api-key' | 'authenticated';
index: string;
deleteOldLogs: boolean;
deleteOldLogsDays: number;
Expand Down
4 changes: 3 additions & 1 deletion src/config/configuration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,9 @@ const configuration: Configuration = {
paymentMethodTypes: ['card'],
},
tracking: {
mode: process.env.TRACKING_MODE === 'all' ? 'all' : 'api-key',
mode:
(process.env.TRACKING_MODE as Configuration['tracking']['mode']) ??
'api-key',
index: process.env.TRACKING_INDEX ?? 'staart-logs',
deleteOldLogs:
process.env.TRACKING_DELETE_OLD_LOGS !== undefined
Expand Down
8 changes: 7 additions & 1 deletion src/middleware/api-logger.middleware.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Injectable, NestMiddleware } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { NextFunction, Request, Response } from 'express';
import { validate } from 'uuid';
import { Configuration } from '../config/configuration.interface';
import { ElasticSearchService } from '../providers/elasticsearch/elasticsearch.service';

Expand Down Expand Up @@ -28,7 +29,12 @@ export class ApiLoggerMiddleware implements NestMiddleware {
};
if (config.mode === 'all')
this.elasticSearchService.index(config.index, obj);
else if (config.mode === 'api-key' && req.headers.authorization)
else if (config.mode === 'authenticated' && req.headers.authorization)
this.elasticSearchService.index(config.index, obj);
else if (
config.mode === 'api-key' &&
validate(req.headers.authorization?.replace('Bearer ', ''))
)
this.elasticSearchService.index(config.index, obj);
});
next();
Expand Down

0 comments on commit f50a401

Please sign in to comment.