From 6cfd353a54a8d0f66fe5271351c3b44deca1287e Mon Sep 17 00:00:00 2001 From: tycrek Date: Wed, 7 Dec 2022 13:24:03 -0700 Subject: [PATCH] feat: added export for setting passwords --- src/auth.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/auth.ts b/src/auth.ts index 69c3b26..7bb8df1 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -92,6 +92,22 @@ export const createNewUser = (username: string, passhash: string, admin: boolean fs.writeJson(authPath, authData, { spaces: '\t' }); }); +export const setUserPassword = (unid: string, password: string): Promise => new Promise(async (resolve, reject) => { + + // Find the user + const user = users.find((user) => user.unid === unid); + if (!user) return reject(new Error('User not found')); + + // Set the password + user.passhash = await bcrypt.hash(password, 10); + + // Save the new user to auth.json + const authPath = path('auth.json'); + const authData = fs.readJsonSync(authPath) as Users; + const userIndex = authData.users.findIndex((user) => user.unid === unid); + authData.users[userIndex] = user; + fs.writeJson(authPath, authData, { spaces: '\t' }); +}); /** * Called by ass.ts on startup