From 5e63c9621b0b60950a8dc3ce48d5712e4b00ac16 Mon Sep 17 00:00:00 2001 From: Carifio24 Date: Tue, 10 Oct 2023 17:13:26 -0400 Subject: [PATCH] Allow requests to /, but mention that an API key is needed if one wasn't provided. --- src/server.ts | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/src/server.ts b/src/server.ts index 4c8f4ff..9030668 100644 --- a/src/server.ts +++ b/src/server.ts @@ -110,6 +110,11 @@ const store = new SequelizeStore({ async function apiKeyMiddleware(req: Request, res: ExpressResponse, next: NextFunction): Promise { + if (req.originalUrl === "/") { + next(); + return; + } + // The whitelisting of hosts is temporary! const host = req.headers.origin; const validOrigin = host && ALLOWED_ORIGINS.includes(host); @@ -172,8 +177,15 @@ app.all("*", (req, _res, next) => { }); // simple route -app.get("/", (_req, res) => { - res.json({ message: "Welcome to the CosmicDS server." }); +app.get("/", async (req, res) => { + const key = req.get("Authorization"); + const apiKey = key ? await getAPIKey(key) : null; + const apiKeyExists = apiKey !== null; + let message = "Welcome to the CosmicDS server!"; + if (!apiKeyExists) { + message += " You'll need to include a valid API key with your requests in order to access other endpoints."; + } + res.json({ message: message }); }); function _sendUserIdCookie(userId: number, res: ExpressResponse): void {