public async changePassword()

in src/components/users/change-password/ko/runtime/change-password.ts [91:160]


    public async changePassword(): Promise<void> {
        const captchaIsRequired = this.requireHipCaptcha();
        const validationGroup = {
            password: this.password,
            newPassword: this.newPassword,
            passwordConfirmation: this.passwordConfirmation
        };

        if (captchaIsRequired) {
            if (!this.setCaptchaValidation) {
                this.logger.trackEvent("CaptchaValidation", { message: "Captcha failed to initialize." });
                dispatchErrors(this.eventManager, ErrorSources.resetpassword, [ValidationMessages.captchaNotInitialized]);
                return;
            }

            validationGroup["captcha"] = this.captcha;
            this.setCaptchaValidation(this.captcha);
        }

        const result = validation.group(validationGroup);

        const clientErrors = result();

        if (clientErrors.length > 0) {
            result.showAllMessages();
            dispatchErrors(this.eventManager, ErrorSources.changepassword, clientErrors);
            return;
        }

        const user = await this.usersService.getCurrentUser();
        const credentials = `Basic ${Buffer.from(`${user.email}:${this.password()}`, "utf8").toString("base64")}`;
        let userId = await this.usersService.authenticate(credentials);

        if (!userId) {
            dispatchErrors(this.eventManager, ErrorSources.changepassword, ["Incorrect user name or password"]);
            return;
        }

        userId = `/users/${userId}`;

        try {
            this.working(true);
            dispatchErrors(this.eventManager, ErrorSources.changepassword, []);

            if (captchaIsRequired) {
                const captchaRequestData = this.captchaData();
                const resetRequest: ChangePasswordRequest = {
                    challenge: captchaRequestData.challenge,
                    solution: captchaRequestData.solution?.solution,
                    flowId: captchaRequestData.solution?.flowId,
                    token: captchaRequestData.solution?.token,
                    type: captchaRequestData.solution?.type,
                    userId: userId,
                    newPassword: this.newPassword()
                };
                await this.backendService.sendChangePassword(resetRequest, credentials);
            } else {
                await this.usersService.changePassword(userId, this.newPassword(), credentials);
            }
            this.isChangeConfirmed(true);
        } catch (error) {
            if (captchaIsRequired) {
                await this.refreshCaptcha();
            }

            parseAndDispatchError(this.eventManager, ErrorSources.changepassword, error, this.logger, undefined, detail => `${detail.target}: ${detail.message} \n`);
        } finally {
            this.working(false);
        }
    }