From 4cb47f3c7cac7b2c41152483c116fba594783861 Mon Sep 17 00:00:00 2001 From: Matchu Date: Mon, 12 Apr 2021 18:05:00 -0700 Subject: [PATCH] Don't crash if auth0 credentials are missing I'm setting up the app in a fresh box, and I noticed that the Auth0 credentials are an immediate crasher if not present. That doesn't seem ideal to me for something only used in support actions! I'd rather just have that support mutation crash, if we happen to call it. --- src/server/types/MutationsForSupport.js | 25 ++++++++++++++++++------- 1 file changed, 18 insertions(+), 7 deletions(-) diff --git a/src/server/types/MutationsForSupport.js b/src/server/types/MutationsForSupport.js index f219d37..ae1a008 100644 --- a/src/server/types/MutationsForSupport.js +++ b/src/server/types/MutationsForSupport.js @@ -11,12 +11,23 @@ import { normalizeRow, } from "../util"; -const auth0 = new ManagementClient({ - domain: "openneo.us.auth0.com", - clientId: process.env.AUTH0_SUPPORT_CLIENT_ID, - clientSecret: process.env.AUTH0_SUPPORT_CLIENT_SECRET, - scope: "read:users update:users", -}); +let auth0; +/** + * Get an Auth0 client. Raises a runtime error if connection fails. (That way, + * we don't crash on server startup, just for this Support-only feature!) + */ +function getAuth0() { + if (!auth0) { + auth0 = new ManagementClient({ + domain: "openneo.us.auth0.com", + clientId: process.env.AUTH0_SUPPORT_CLIENT_ID, + clientSecret: process.env.AUTH0_SUPPORT_CLIENT_SECRET, + scope: "read:users update:users", + }); +} + + return auth0; +} const typeDefs = gql` type RemoveLayerFromItemMutationResult { @@ -838,7 +849,7 @@ const resolvers = { // I'm not going to bother to write recovery code; in that case, the // error will reach the support user console, and we can work to manually // fix it. - await auth0.users.update( + await getAuth0().users.update( { id: `auth0|impress-${userId}` }, { username: newUsername } );