From 7b6e2b12e176cdfcd0db4a9c504116838fdb6e14 Mon Sep 17 00:00:00 2001 From: tycrek Date: Sat, 24 Dec 2022 21:28:15 -0700 Subject: [PATCH] feat: create default users --- src/auth.ts | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/src/auth.ts b/src/auth.ts index 384fabe..4429ecc 100644 --- a/src/auth.ts +++ b/src/auth.ts @@ -92,7 +92,7 @@ const migrate = (authFileName = 'auth.json'): Promise => new Promise(asyn }); /** - * This is a WIP + * Creates a new user account */ export const createNewUser = (username: string, password: string, admin: boolean, meta?: { [key: string]: any }): Promise => new Promise(async (resolve, reject) => { @@ -112,7 +112,12 @@ export const createNewUser = (username: string, password: string, admin: boolean // Save the new user to auth.json const authPath = path('auth.json'); const authData = fs.readJsonSync(authPath) as Users; + + if (!authData.users) authData.users = []; authData.users.push(newUser); + + if (!authData.meta) authData.meta = {}; + fs.writeJson(authPath, authData, { spaces: '\t' }) .then(() => resolve(newUser)) .catch(reject); @@ -166,16 +171,16 @@ export const onStart = (authFile = 'auth.json') => new Promise((resolve, reject) migrate(authFile)); else return json; }) - .then((json: Users) => { + .then(async (json) => { // Check if the file is empty - if (Object.keys(json).length === 0) { + if (!json.users || json.users.length === 0) { log.debug('auth.json is empty, creating default user'); - //return createDefaultUser(); // todo: need to do this + return await createNewUser('ass', nanoid(), true); } // Add users to the map - json.users.forEach((user) => users.push(user)); + return json.users.forEach((user) => users.push(user)); }) .catch((errReadJson) => log.error('Failed to read auth.json').callback(reject, errReadJson)) .then(resolve);