uptime-kuma/extra/reset-password.js

76 lines
2.1 KiB
JavaScript
Raw Normal View History

2021-08-09 01:34:44 -04:00
console.log("== Uptime Kuma Reset Password Tool ==");
const Database = require("../server/database");
const { R } = require("redbean-node");
const readline = require("readline");
const { initJWTSecret } = require("../server/util-server");
2022-04-18 03:21:58 -04:00
const User = require("../server/model/user");
2021-09-20 04:29:18 -04:00
const args = require("args-parser")(process.argv);
2021-08-09 01:34:44 -04:00
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
2021-10-15 13:33:44 -04:00
const main = async () => {
console.log("Connecting the database");
Database.initDataDir(args);
await Database.connect(false, false, true);
2021-08-09 01:34:44 -04:00
try {
2021-10-15 13:33:44 -04:00
// 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) {
2021-10-16 01:02:04 -04:00
const user = await R.findOne("user");
2021-10-15 13:33:44 -04:00
if (! user) {
throw new Error("user not found, have you installed?");
}
2021-08-09 01:34:44 -04:00
2021-10-15 13:33:44 -04:00
console.log("Found user: " + user.username);
2021-08-09 01:34:44 -04:00
2021-10-15 13:33:44 -04:00
while (true) {
let password = await question("New Password: ");
let confirmPassword = await question("Confirm New Password: ");
2021-08-09 01:34:44 -04:00
2021-10-15 13:33:44 -04:00
if (password === confirmPassword) {
2022-04-18 03:21:58 -04:00
await User.resetPassword(user.id, password);
2021-08-09 01:34:44 -04:00
2021-10-15 13:33:44 -04:00
// Reset all sessions by reset jwt secret
await initJWTSecret();
2021-08-09 01:34:44 -04:00
2021-10-15 13:33:44 -04:00
break;
} else {
console.log("Passwords do not match, please try again.");
}
2021-08-09 01:34:44 -04:00
}
2021-10-15 13:33:44 -04:00
console.log("Password reset successfully.");
2021-08-09 01:34:44 -04:00
}
} catch (e) {
console.error("Error: " + e.message);
}
await Database.close();
2021-10-15 13:33:44 -04:00
rl.close();
2021-08-09 01:34:44 -04:00
2021-10-15 13:33:44 -04:00
console.log("Finished.");
};
2021-08-09 01:34:44 -04:00
/**
* Ask question of user
* @param {string} question Question to ask
* @returns {Promise<string>} Users response
*/
2021-08-09 01:34:44 -04:00
function question(question) {
return new Promise((resolve) => {
rl.question(question, (answer) => {
resolve(answer);
2021-10-15 13:33:44 -04:00
});
2021-08-09 01:34:44 -04:00
});
}
2021-10-15 13:33:44 -04:00
if (!process.env.TEST_BACKEND) {
main();
}
module.exports = {
main,
};