@samitouri / QOSami-HFS / commits / 54ab3d90

create-admin will update the existing account, if any

Massimo Melina committed Apr 30, 2024 at 18:52 UTC 54ab3d9071ed3324ad49ec43489b9948412b6489
1 file changed +11 -10
src/perm.ts
+11 -10
@@ -58,9 +58,8 @@ createAdminConfig.sub(v => {
58 })
59
60 export async function createAdmin(password: string, username='admin') {
61 - const acc = await addAccount(username, { admin: true, password })
62 - if (!acc) return console.log("cannot create, already exists")
63 - console.log("account admin created")
61 + const acc = await addAccount(username, { admin: true, password }, true)
62 + console.log(acc ? "account admin created" : "something went wrong")
63 }
64
65 const srp6aNimbusRoutines = new SRPRoutines(new SRPParameters())
@@ -142,15 +141,17 @@ export function renameAccount(from: string, to: string) {
141 }
142 }
143
145 -export async function addAccount(username: string, props: Partial<Account>) {
144 +export async function addAccount(username: string, props: Partial<Account>, updateExisting=false) {
145 username = normalizeUsername(username)
147 - if (!username || getAccount(username, false))
148 - return
149 - const newAccount: Account = setHidden({}, { username }) // have the field in the object but hidden so that stringification won't include it
146 + if (!username) return
147 + let account = getAccount(username, false)
148 + if (account && !updateExisting) return
149 + account = setHidden(account || {}, { username }) // hidden so that stringification won't include it
150 + Object.assign(account, _.pickBy(props, Boolean))
151 accountsConfig.set(accounts =>
151 - Object.assign(accounts, { [username]: newAccount }))
152 - await updateAccount(newAccount, props)
153 - return newAccount
152 + Object.assign(accounts, { [username]: account }))
153 + await updateAccount(account, account)
154 + return account
155 }
156
157 export function delAccount(username: string) {