Repository: louislam/uptime-kuma
File: extra/reset-password.js (JavaScript)

Code before:
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.initDataDir(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);

            while (true) {
                let password;
                let confirmPassword;

                # When called with "--new_password" argument for unattended modification (e.g. npm run reset-password -- --new_password=secret) 
                if ('new_password' in args) {
                    console.log("Note: Using password from argument");
                    password = confirmPassword = args.new_password;
                } else {
                    password = await question("New Password: ");
                    confirmPassword = await question("Confirm New Password: ");
                }

                if (password === confirmPassword) {
                    await User.resetPassword(user.id, password);

                    // Reset all sessions by reset jwt secret
                    await initJWTSecret();

                    break;
                } else {
                    console.log("Passwords do not match, please try again.");
                }
            }
            console.log("Password reset successfully.");
        }
    } catch (e) {
        console.error("Error: " + e.message);
    }


Review comment (security):
Please test the contribution before creating a PR.

This won't run, as JS usess backslashes
Please make shure that you have run the formatter and have tested the contribution

See 
![image](https://github.com/louislam/uptime-kuma/assets/26258709/1af7b052-fbd5-4485-ac82-72f4b955286f)


```suggestion
                // When called with "--new_password" argument for unattended modification (e.g. npm run reset-password -- --new_password=secret) 
```

Code after:
const args = require("args-parser")(process.argv);
const rl = readline.createInterface({
    input: process.stdin,
    output: process.stdout
});

const main = async () => {
    if ("dry-run" in args) {
        console.log("Dry run mode, no changes will be made.");
    }

    console.log("Connecting the database");
    Database.initDataDir(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);

            while (true) {
                let password;
                let confirmPassword;

                // When called with "--new-password" argument for unattended modification (e.g. npm run reset-password -- --new_password=secret)
                if ("new-password" in args) {
                    console.log("Using password from argument");
                    console.warn("\x1b[31m%s\x1b[0m", "Warning: the password might be stored, in plain text, in your shell's history");
                    password = confirmPassword = args["new-password"] + "";
                } else {
                    password = await question("New Password: ");
                    confirmPassword = await question("Confirm New Password: ");
                }

                if (password === confirmPassword) {
                    if (!("dry-run" in args)) {
                        await User.resetPassword(user.id, password);

                        // Reset all sessions by reset jwt secret
                        await initJWTSecret();
                    }
                    break;
                } else {
                    console.log("Passwords do not match, please try again.");
                }
            }