Repository: louislam/uptime-kuma
File: src/pages/Settings.vue (Vue)

Code before:
    },

    methods: {

        saveGeneral() {
            localStorage.timezone = this.$root.userTimezone;
            this.saveSettings();
        },

        savePassword() {
            if (this.password.newPassword !== this.password.repeatNewPassword) {
                this.invalidPassword = true;
            } else {
                this.$root.getSocket().emit("changePassword", this.password, (res) => {
                    this.$root.toastRes(res);
                    if (res.ok) {
                        this.password.currentPassword = "";
                        this.password.newPassword = "";
                        this.password.repeatNewPassword = "";
                    }
                });
            }
        },

        loadUsername() {
            const jwtToken = localStorage.getItem("token");
            const [, payloadBase64] = jwtToken.match(/^\S+\.(\S+)\.\S+$/i);
            const payload = atob(payloadBase64);
            const oPayload = JSON.parse(payload);
            this.username = oPayload.username;
        },

        loadSettings() {
            this.$root.getSocket().emit("getSettings", (res) => {
                this.settings = res.data;

                if (this.settings.searchEngineIndex === undefined) {
                    this.settings.searchEngineIndex = false;
                }

                if (this.settings.entryPage === undefined) {
                    this.settings.entryPage = "dashboard";
                }

                if (this.settings.keepDataPeriodDays === undefined) {
                    this.settings.keepDataPeriodDays = 180;
                }

                this.loaded = true;
            });
        },

Review comment (suggestion):
localStorage is not always correct if user do not tick "Remember me". Should use `this.$root.storage().token` instead.

Code after:
        this.loadSettings();
    },

    methods: {

        saveGeneral() {
            localStorage.timezone = this.$root.userTimezone;
            this.saveSettings();
        },

        savePassword() {
            if (this.password.newPassword !== this.password.repeatNewPassword) {
                this.invalidPassword = true;
            } else {
                this.$root.getSocket().emit("changePassword", this.password, (res) => {
                    this.$root.toastRes(res);
                    if (res.ok) {
                        this.password.currentPassword = "";
                        this.password.newPassword = "";
                        this.password.repeatNewPassword = "";
                    }
                });
            }
        },

        loadUsername() {
            const jwtToken = this.$root.storage().token;
            const jwtPayload = jwt_decode(jwtToken);
            this.username = jwtPayload.username;
        },

        loadSettings() {
            this.$root.getSocket().emit("getSettings", (res) => {
                this.settings = res.data;

                if (this.settings.searchEngineIndex === undefined) {
                    this.settings.searchEngineIndex = false;
                }

                if (this.settings.entryPage === undefined) {
                    this.settings.entryPage = "dashboard";
                }

                if (this.settings.keepDataPeriodDays === undefined) {
                    this.settings.keepDataPeriodDays = 180;
                }

                this.loaded = true;
            });
        },