From 2be4d913cc0d69f1522452c74fff1a5969d395ea Mon Sep 17 00:00:00 2001
From: Gowtham Sundaresan
 <131300352+gowthamsundaresan@users.noreply.github.com>
Date: Thu, 19 Dec 2024 20:05:46 +0530
Subject: [PATCH] [Patch] - Server auth optimizations (#314)

* perf: remove extra fetch when pagination not required

* chore: change refresh-store route from get to post
---
 packages/api/src/routes/auth/authController.ts | 2 +-
 packages/api/src/routes/auth/authRoutes.ts     | 2 +-
 packages/api/src/utils/authMiddleware.ts       | 3 +++
 3 files changed, 5 insertions(+), 2 deletions(-)

diff --git a/packages/api/src/routes/auth/authController.ts b/packages/api/src/routes/auth/authController.ts
index c66af47c..b4b0827b 100644
--- a/packages/api/src/routes/auth/authController.ts
+++ b/packages/api/src/routes/auth/authController.ts
@@ -152,7 +152,7 @@ export async function registerUser(req: Request, res: Response) {
 }
 
 /**
- * Protected route, refreshes the server's entire auth store. Called by Supabase edge fn signal-refresh.
+ * Protected route, refreshes the server's entire auth store. Called by Supabase edge fn signal-refresh
  * This function will fail if the caller does not use admin-level auth token
  *
  * @param req
diff --git a/packages/api/src/routes/auth/authRoutes.ts b/packages/api/src/routes/auth/authRoutes.ts
index cfbddca4..8817e995 100644
--- a/packages/api/src/routes/auth/authRoutes.ts
+++ b/packages/api/src/routes/auth/authRoutes.ts
@@ -7,7 +7,7 @@ const router = express.Router()
 
 // API routes for /auth
 
-router.get('/refresh-store', routeCache.cacheSeconds(5), signalRefreshAuthStore)
+router.post('/refresh-store', signalRefreshAuthStore)
 router.get('/users/:address/check-status', routeCache.cacheSeconds(30), checkUserStatus)
 router.get('/users/:address/nonce', routeCache.cacheSeconds(10), generateNonce)
 router.post('/users/:address/register', routeCache.cacheSeconds(10), registerUser)
diff --git a/packages/api/src/utils/authMiddleware.ts b/packages/api/src/utils/authMiddleware.ts
index 98d012df..00fe9cbf 100644
--- a/packages/api/src/utils/authMiddleware.ts
+++ b/packages/api/src/utils/authMiddleware.ts
@@ -252,6 +252,9 @@ export async function refreshAuthStore() {
 					)
 				}
 			}
+
+			if (users.length < take) break
+
 			skip += take
 		}