From d5cc6d506760c5de7f25690ec054df15da9dba3d Mon Sep 17 00:00:00 2001 From: James Grugett Date: Mon, 16 May 2022 20:26:51 -0400 Subject: [PATCH] Revert "Better random (#213)" This reverts commit c9f36449884c887d6dd5443e3f44ccdd9f5e38e8. --- common/util/random.ts | 12 ++++-------- functions/src/create-user.ts | 3 ++- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/common/util/random.ts b/common/util/random.ts index 3026dcde..f52294f1 100644 --- a/common/util/random.ts +++ b/common/util/random.ts @@ -1,11 +1,7 @@ -// Returns a cryptographically random hexadecimal string of length `length` -// (thus containing 4*`length` bits of entropy). -export const randomString = (length = 12) => { - const bytes = new Uint8Array(Math.ceil(length / 2)) - crypto.getRandomValues(bytes) - const hex = bytes.reduce((s, b) => s + ('0' + b.toString(16)).slice(-2), '') - return hex.substring(0, length) -} +export const randomString = (length = 12) => + Math.random() + .toString(16) + .substring(2, length + 2) export function genHash(str: string) { // xmur3 diff --git a/functions/src/create-user.ts b/functions/src/create-user.ts index fdbb0edd..f73b868b 100644 --- a/functions/src/create-user.ts +++ b/functions/src/create-user.ts @@ -42,7 +42,8 @@ export const createUser = functions const name = cleanDisplayName(rawName) let username = cleanUsername(name) - while (await getUserByUsername(username)) { + const sameNameUser = await getUserByUsername(username) + if (sameNameUser) { username += randomString(4) }