@@ -14,6 +14,12 @@ const getPopularEndpoints = async (req, res, next) => {
14
14
try {
15
15
const { top } = req . query ;
16
16
const [ start , end ] = parseTopParam ( top ) ;
17
+ const key = req . headers . key ;
18
+
19
+ // Check for valid access key in headers
20
+ if ( ! key || key !== process . env . ACCESS_KEY ) {
21
+ return res . status ( 401 ) . json ( { message : 'Unauthorized' } ) ;
22
+ }
17
23
18
24
// ✅ Use .lean() to get a plain object
19
25
const stats = await Stat . findOne ( { _id : 'system' } , { endpoints : 1 } ) . lean ( ) ;
@@ -38,6 +44,12 @@ const getTopEndpointsToday = async (req, res, next) => {
38
44
const today = new Date ( ) . toISOString ( ) . split ( 'T' ) [ 0 ] ; // YYYY-MM-DD format
39
45
const { top } = req . query ;
40
46
const [ start , end ] = parseTopParam ( top ) ;
47
+ const key = req . headers . key ;
48
+
49
+ // Check for valid access key in headers
50
+ if ( ! key || key !== process . env . ACCESS_KEY ) {
51
+ return res . status ( 401 ) . json ( { message : 'Unauthorized' } ) ;
52
+ }
41
53
42
54
// ✅ Use .lean() to get a plain object
43
55
const stats = await Stat . findOne ( { _id : 'system' } , { daily : 1 } ) . lean ( ) ;
@@ -69,6 +81,12 @@ const getMonthlyRequests = async (req, res, next) => {
69
81
try {
70
82
const now = new Date ( ) ;
71
83
const last5Months = [ ] ;
84
+ const key = req . headers . key ;
85
+
86
+ // Check for valid access key in headers
87
+ if ( ! key || key !== process . env . ACCESS_KEY ) {
88
+ return res . status ( 401 ) . json ( { message : 'Unauthorized' } ) ;
89
+ }
72
90
73
91
for ( let i = 0 ; i < 5 ; i ++ ) {
74
92
const date = new Date ( Date . UTC ( now . getUTCFullYear ( ) , now . getUTCMonth ( ) - i , 1 ) ) ; // Ensure UTC consistency
0 commit comments