fix: API add_account was not storing password correctly, so account wasn't active until restart
Massimo Melina committed
Apr 23, 2024 at 15:49 UTC
05abe33225c16f783732b844f52571532c1c50ae
2 files changed
+7
-8
src/api.accounts.ts
+2
-2
@@ -57,8 +57,8 @@ export default {
57
await updateAccount(existing, rest)
58
return _.pick(existing, 'username')
59
}
60
- const acc = addAccount(username, rest)
61
- return acc ? _.pick(acc, 'username') : new ApiError(HTTP_BAD_REQUEST)
60
+ const acc = await addAccount(username, rest)
61
+ return acc ? _.pick(acc, 'username') : new ApiError(HTTP_BAD_REQUEST) // return username because it is normalized
62
},
63
64
del_account({ username }) {
src/perm.ts
+5
-6
@@ -56,10 +56,9 @@ createAdminConfig.sub(v => {
56
createAdminConfig.set('')
57
})
58
59
-export function createAdmin(password: string, username='admin') {
60
- const acc = addAccount(username, { admin: true })
59
+export async function createAdmin(password: string, username='admin') {
60
+ const acc = await addAccount(username, { admin: true, password })
61
if (!acc) return console.log("cannot create, already exists")
62
- updateAccount(acc!, { password })
62
console.log("account admin created")
63
}
64
@@ -92,7 +91,7 @@ export async function updateAccount(account: Account, change: Partial<Account> |
91
account.expire &&= new Date(account.expire)
92
if (username !== usernameWas)
93
renameAccount(usernameWas, username)
95
- if (jsonWas !== JSON.stringify(account))
94
+ if (jsonWas !== JSON.stringify(account)) // this test will miss the 'username' field, because hidden, but renameAccount is already calling saveAccountsASAP
95
saveAccountsAsap()
96
}
97
@@ -141,14 +140,14 @@ export function renameAccount(from: string, to: string) {
140
}
141
}
142
144
-export function addAccount(username: string, props: Partial<Account>) {
143
+export async function addAccount(username: string, props: Partial<Account>) {
144
username = normalizeUsername(username)
145
if (!username || getAccount(username, false))
146
return
147
const copy: Account = setHidden(_.pickBy(props, Boolean), { username }) // have the field in the object but hidden so that stringification won't include it
148
accountsConfig.set(accounts =>
149
Object.assign(accounts, { [username]: copy }))
151
- saveAccountsAsap()
150
+ await updateAccount(copy, copy)
151
return copy
152
}
153