@@ -14,6 +14,12 @@ const getPopularEndpoints = async (req, res, next) => {
1414 try {
1515 const { top } = req . query ;
1616 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+ }
1723
1824 // ✅ Use .lean() to get a plain object
1925 const stats = await Stat . findOne ( { _id : 'system' } , { endpoints : 1 } ) . lean ( ) ;
@@ -38,6 +44,12 @@ const getTopEndpointsToday = async (req, res, next) => {
3844 const today = new Date ( ) . toISOString ( ) . split ( 'T' ) [ 0 ] ; // YYYY-MM-DD format
3945 const { top } = req . query ;
4046 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+ }
4153
4254 // ✅ Use .lean() to get a plain object
4355 const stats = await Stat . findOne ( { _id : 'system' } , { daily : 1 } ) . lean ( ) ;
@@ -69,6 +81,12 @@ const getMonthlyRequests = async (req, res, next) => {
6981 try {
7082 const now = new Date ( ) ;
7183 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+ }
7290
7391 for ( let i = 0 ; i < 5 ; i ++ ) {
7492 const date = new Date ( Date . UTC ( now . getUTCFullYear ( ) , now . getUTCMonth ( ) - i , 1 ) ) ; // Ensure UTC consistency
0 commit comments