From 4daf04183dd9b38183aae80f1c25cc7fe2681563 Mon Sep 17 00:00:00 2001 From: Chase Manning Date: Sun, 7 Jan 2024 19:07:22 +0000 Subject: [PATCH 1/2] fail silently for discord issues --- functions/src/registration.ts | 31 +++++++++++++++++++++---------- 1 file changed, 21 insertions(+), 10 deletions(-) diff --git a/functions/src/registration.ts b/functions/src/registration.ts index 479bfcf..5e2fc5b 100644 --- a/functions/src/registration.ts +++ b/functions/src/registration.ts @@ -37,13 +37,14 @@ async function getDiscordData(code: string, secrets: Secrets): Promise<{ usernam export default async function registrationHandler(request: Request, secrets: Secrets) { const params = validateParams(request.body, ...requiredKeys); const address = getUserAddress(params.signature); - const db = admin.database(); + // Validating address if (await userExists(address)) { throw new APIError("Address already used"); } + // Validating invite code const codeSnapshot = await db.ref("invites").child(params.inviteCode).get(); if (!codeSnapshot.exists()) { throw new APIError("Invalid invite code"); @@ -52,25 +53,35 @@ export default async function registrationHandler(request: Request, secrets: Sec throw new APIError("Code already used"); } + // Validating Twitter const twitterUsername = await getTwitterUsername(params.twitterCode, secrets); - if (await usernameExists("twitterUsername", twitterUsername)) { throw new APIError("Twitter already used"); } - const { username: discordUsername, guilds } = await getDiscordData(params.discordCode, secrets); - - if (await usernameExists("discordUsername", discordUsername)) { - throw new APIError("Discord already used"); - } - - if (!guilds.some((guild) => guild.id === tlxGuidID)) { - throw new APIError("Not in the TLX Discord server"); + // Validating Discord + let discordUsername = "ERROR"; + try { + const { username, guilds } = await getDiscordData(params.discordCode, secrets); + discordUsername = username; + if (await usernameExists("discordUsername", discordUsername)) { + throw new APIError("Discord already used"); + } + if (!guilds.some((guild) => guild.id === tlxGuidID)) { + throw new APIError("Not in the TLX Discord server"); + } + } catch (error) { + logger.log(`Discord validation failed: ${error}`); } + // Saving user const user = { twitterUsername, discordUsername }; await db.ref("users").child(address).set(user); + + // Generating invite codes const codes = await generateAndSaveInviteCodes(address, invitesPerUser); + + // Using invite code await useCode(params.inviteCode, address); return { address, ...user, codes }; From 4a02b60719b386323220ad916cdb539c8a3e6c30 Mon Sep 17 00:00:00 2001 From: Chase Manning Date: Sun, 7 Jan 2024 19:20:32 +0000 Subject: [PATCH 2/2] fix issue with errors not propagating --- functions/src/registration.ts | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/functions/src/registration.ts b/functions/src/registration.ts index 5e2fc5b..fbf0d80 100644 --- a/functions/src/registration.ts +++ b/functions/src/registration.ts @@ -21,16 +21,20 @@ async function getTwitterUsername(code: string, secrets: Secrets): Promise { +async function getDiscordData( + code: string, + secrets: Secrets +): Promise<{ username: string; guilds: Guild[]; error: boolean }> { try { logger.info("Getting Discord data"); logger.info(`Code: ${code}`); const discordService = await DiscordService.fromCode(code, secrets); const username = await discordService.getUsername(); const guilds = await discordService.getGuilds(); - return { username, guilds }; + return { username, guilds, error: false }; } catch (error) { - throw new APIError(`Discord authentication failed: ${error}`); + logger.error(`Discord authentication failed: ${error}`); + return { username: "ERROR", guilds: [], error: true }; } } @@ -60,18 +64,14 @@ export default async function registrationHandler(request: Request, secrets: Sec } // Validating Discord - let discordUsername = "ERROR"; - try { - const { username, guilds } = await getDiscordData(params.discordCode, secrets); - discordUsername = username; + const { username: discordUsername, guilds, error } = await getDiscordData(params.discordCode, secrets); + if (!error) { if (await usernameExists("discordUsername", discordUsername)) { throw new APIError("Discord already used"); } if (!guilds.some((guild) => guild.id === tlxGuidID)) { throw new APIError("Not in the TLX Discord server"); } - } catch (error) { - logger.log(`Discord validation failed: ${error}`); } // Saving user