diff options
-rwxr-xr-x | cli.js | 2 | ||||
-rw-r--r-- | db.js | 2 | ||||
-rw-r--r-- | superadmin/index.js | 12 | ||||
-rw-r--r-- | superadmin/public/index.html | 6 | ||||
-rw-r--r-- | superadmin/public/script.js | 23 |
5 files changed, 43 insertions, 2 deletions
@@ -151,7 +151,7 @@ if ((idx = params.indexOf("-r")) > -1) { const uid = params[idx + 1]; if (!uid) process.exit(1); db.regenerateUser(uid) - .then(() => console.info("Regenerating user with id " + uid)) + .then((pwd) => console.info(`Regenerating user with id ${uid}: ${pwd}`)) .then(() => process.exit(0)) .catch(console.error); } else { @@ -230,7 +230,7 @@ class DB { const pwd = nanoid.nanoid(8); const password = await bcrypt.hash(pwd, 10); await this.query("UPDATE users SET password = ? WHERE id = ?", [password, uid]); - console.log(`New password for ${uid}: ${pwd}`); + return pwd; } async dump() { diff --git a/superadmin/index.js b/superadmin/index.js index 2518f97..9498327 100644 --- a/superadmin/index.js +++ b/superadmin/index.js @@ -27,4 +27,16 @@ app.get("/api/pull", checkSuperAdmin, (req, res) => { }); }); +app.post("/api/reset", checkSuperAdmin, async (req, res) => { + const { uid } = req.body; + if (!uid) return res.json({ success: false }); + try { + const pwd = await db.regenerateUser(uid); + return res.json({ success: true, uid, pwd }); + } catch (e) { + console.error(e); + return res.json({ success: false, e }); + } +}); + module.exports = app;
\ No newline at end of file diff --git a/superadmin/public/index.html b/superadmin/public/index.html index 0085630..f42a977 100644 --- a/superadmin/public/index.html +++ b/superadmin/public/index.html @@ -25,6 +25,12 @@ <div id="pull-response"></div> </div> <div> + <h3>Reset password</h3> + <input type="number" id="reset-input" placeholder="72" /> + <button class="pure-button pure-button-primary" id="reset-button">Reset</button> + <div id="reset-response"></div> + </div> + <div> <h3>SQL Select</h3> <form class="pure-form" id="query-form"> <textarea required placeholder="SELECT something"></textarea> diff --git a/superadmin/public/script.js b/superadmin/public/script.js index c3f9c0f..e0c5efe 100644 --- a/superadmin/public/script.js +++ b/superadmin/public/script.js @@ -1,6 +1,10 @@ const pullButton = document.getElementById("pull-button"); const pullResponse = document.getElementById("pull-response"); +const resetInput = document.getElementById("reset-input"); +const resetButton = document.getElementById("reset-button"); +const resetResponse = document.getElementById("reset-response"); + const queryForm = document.getElementById("query-form"); const queryResponse = document.getElementById("query-response"); @@ -15,6 +19,25 @@ pullButton.addEventListener("click", async e => { } }); +resetButton.addEventListener("click", async e => { + const uid = resetInput.value; + const body = JSON.stringify({ uid }); + const method = "POST"; + const resp = await fetch("api/reset", { method, body, headers: { "Content-Type": "application/json" } }); + const res = await resp.json(); + while (resetResponse.children.length > 0) resetResponse.removeChild(resetResponse.children[0]); + if (res.success) { + const b = document.createElement("b"); + b.textContent = `${res.uid}: `; + const span = document.createElement("span"); + span.textContent = res.pwd; + resetResponse.append(b, span); + } else { + console.log(res); + resetResponse.textContent = JSON.stringify(res.e); + } +}); + queryForm.addEventListener("submit", async e => { e.preventDefault(); const textarea = queryForm.querySelector("textarea"); |