diff --git a/extra/change-username.js b/extra/change-username.js new file mode 100644 index 00000000..dfa91577 --- /dev/null +++ b/extra/change-username.js @@ -0,0 +1,65 @@ +console.log("== Uptime Kuma Change Username Tool =="); + +const Database = require("../server/database"); +const { R } = require("redbean-node"); +const readline = require("readline"); +const { initJWTSecret } = require("../server/util-server"); +const User = require("../server/model/user"); +const args = require("args-parser")(process.argv); +const rl = readline.createInterface({ + input: process.stdin, + output: process.stdout +}); + +const main = async () => { + console.log("Connecting the database"); + Database.init(args); + await Database.connect(false, false, true); + + try { + // No need to actually reset the password for testing, just make sure no connection problem. It is ok for now. + if (!process.env.TEST_BACKEND) { + const user = await R.findOne("user"); + if (! user) { + throw new Error("user not found, have you installed?"); + } + + console.log("Found user: " + user.username); + let newUsername = await question("New username: "); + await User.updateUsername(user.id, newUsername); + + // Reset all sessions by reset jwt secret + await initJWTSecret(); + + console.log("Username change successfully."); + } + } catch (e) { + console.error("Error: " + e.message); + } + + await Database.close(); + rl.close(); + + console.log("Finished."); +}; + +/** + * Ask question of user + * @param {string} question Question to ask + * @returns {Promise} Users response + */ +function question(question) { + return new Promise((resolve) => { + rl.question(question, (answer) => { + resolve(answer); + }); + }); +} + +if (!process.env.TEST_BACKEND) { + main(); +} + +module.exports = { + main, +}; diff --git a/package.json b/package.json index 4f4956f6..510e5164 100644 --- a/package.json +++ b/package.json @@ -52,6 +52,7 @@ "setup": "git checkout 1.23.13 && npm ci --production && npm run download-dist", "download-dist": "node extra/download-dist.js", "mark-as-nightly": "node extra/mark-as-nightly.js", + "change-username": "node extra/change-username.js", "reset-password": "node extra/reset-password.js", "remove-2fa": "node extra/remove-2fa.js", "simple-dns-server": "node extra/simple-dns-server.js",