From 73e0e37c668718435c85f35cf20680a71179c872 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Fri, 9 Oct 2020 20:44:35 +0100 Subject: [PATCH] Fixing some bugs with JWT creation not getting an API key. --- packages/server/src/api/controllers/auth.js | 2 +- packages/server/src/utilities/usageQuota.js | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/packages/server/src/api/controllers/auth.js b/packages/server/src/api/controllers/auth.js index c16f76caca..3f824524b0 100644 --- a/packages/server/src/api/controllers/auth.js +++ b/packages/server/src/api/controllers/auth.js @@ -55,7 +55,7 @@ exports.authenticate = async ctx => { } // if in cloud add the user api key if (environment.CLOUD) { - payload.apiKey = getAPIKey(ctx.user.appId) + payload.apiKey = await getAPIKey(ctx.user.appId) } const token = jwt.sign(payload, ctx.config.jwtSecret, { diff --git a/packages/server/src/utilities/usageQuota.js b/packages/server/src/utilities/usageQuota.js index 95fc76140f..11d4757398 100644 --- a/packages/server/src/utilities/usageQuota.js +++ b/packages/server/src/utilities/usageQuota.js @@ -69,13 +69,16 @@ exports.update = async (apiKey, property, usage) => { try { await apiKeyTable.update(buildUpdateParams(apiKey, property, usage)) } catch (err) { + // conditional check means the condition failed, need to check why if (err.code === "ConditionalCheckFailedException") { // get the API key so we can check it const keyObj = await apiKeyTable.get({ primary: apiKey }) // the usage quota or usage limits didn't exist if (keyObj && (keyObj.usageQuota == null || keyObj.usageLimits == null)) { - keyObj.usageQuota = DEFAULT_USAGE - keyObj.usageLimits = DEFAULT_PLAN + keyObj.usageQuota = + keyObj.usageQuota == null ? DEFAULT_USAGE : keyObj.usageQuota + keyObj.usageLimits = + keyObj.usageLimits == null ? DEFAULT_PLAN : keyObj.usageLimits keyObj.quotaReset = getNewQuotaReset() await apiKeyTable.put({ item: keyObj }) return