From 619a30d6850f48507e845395111b2740e84e8cc1 Mon Sep 17 00:00:00 2001 From: tycrek Date: Wed, 7 Dec 2022 14:14:11 -0700 Subject: [PATCH] fix: auth file written before bcrypt Promise resolved --- src/auth.ts | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/auth.ts b/src/auth.ts index c660629..4241e2d 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -43,23 +43,24 @@ const migrate = (): Promise => new Promise(async (resolve, reject) => { newUsers.migrated = true; // Loop through each user - Object.entries(oldUsers).forEach(async ([token, { username }]) => { + await Promise.all(Object.entries(oldUsers).map(async ([token, { username }]) => { // Determine if this user is the admin const admin = Object.keys(oldUsers).indexOf(token) === 0; + const passhash = admin ? await bcrypt.hash(nanoid(32), SALT_ROUNDS) : ''; // Create a new user object const newUser: User = { unid: nanoid(), - username: username, - passhash: admin ? await bcrypt.hash(nanoid(32), SALT_ROUNDS) : '', + username, + passhash, token, admin, meta: {} }; newUsers.users.push(newUser); - }); + })); // Save the new users object to auth.json fs.writeJson(authPath, newUsers, { spaces: '\t' })