Skip to content

Commit

Permalink
[feat] Ability to change username
Browse files Browse the repository at this point in the history
As there is no user management at the moment.
  • Loading branch information
MarcHagen committed Jun 22, 2022
1 parent f9f6414 commit 6d40d30
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 0 deletions.
60 changes: 60 additions & 0 deletions extra/change-username.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
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.");
};

function question(question) {
return new Promise((resolve) => {
rl.question(question, (answer) => {
resolve(answer);
});
});
}

if (!process.env.TEST_BACKEND) {
main();
}

module.exports = {
main,
};
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
"setup": "git checkout 1.16.1 && 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",
"compile-install-script": "@powershell -NoProfile -ExecutionPolicy Unrestricted -Command ./extra/compile-install-script.ps1",
Expand Down
11 changes: 11 additions & 0 deletions server/model/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,17 @@ class User extends BeanModel {
this.password = newPassword;
}

/**
* @param {number} userID
* @param {string} newUsername
* @returns {Promise<void>}
*/
static async updateUsername(userID, newUsername) {
await R.exec("UPDATE `user` SET username = ? WHERE id = ? ", [
newUsername,
userID
]);
}
}

module.exports = User;

0 comments on commit 6d40d30

Please sign in to comment.