Skip to content

Commit

Permalink
Merge pull request #3 from TLX-Protocol/fail-silently-for-discord
Browse files Browse the repository at this point in the history
Fail Silently for Discord Issues
  • Loading branch information
chase-manning authored Jan 7, 2024
2 parents 75971af + 4a02b60 commit 7ee7778
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions functions/src/registration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,29 +21,34 @@ async function getTwitterUsername(code: string, secrets: Secrets): Promise<strin
}
}

async function getDiscordData(code: string, secrets: Secrets): Promise<{ username: string; guilds: Guild[] }> {
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 };
}
}

export default async function registrationHandler(request: Request, secrets: Secrets) {
const params = validateParams<RegistrationParams>(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");
Expand All @@ -52,25 +57,31 @@ 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
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");
}
}

// 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 };
Expand Down

0 comments on commit 7ee7778

Please sign in to comment.