Add change password ui

This commit is contained in:
Sirz Benjie 2025-06-05 15:08:05 -05:00
parent 4f259e2c2f
commit 38ee8ac718
No known key found for this signature in database
GPG Key ID: 4A524B4D196C759E
6 changed files with 154 additions and 1 deletions

View File

@ -15,3 +15,10 @@ export interface AccountRegisterRequest {
username: string; username: string;
password: string; password: string;
} }
export interface AccountChangePwRequest {
password: string;
}
export interface AccountChangePwResponse {
success: boolean;
}

View File

@ -43,5 +43,6 @@ export enum UiMode {
TEST_DIALOGUE, TEST_DIALOGUE,
AUTO_COMPLETE, AUTO_COMPLETE,
ADMIN, ADMIN,
MYSTERY_ENCOUNTER MYSTERY_ENCOUNTER,
CHANGE_PASSWORD_FORM,
} }

View File

@ -1,6 +1,7 @@
import { ApiBase } from "#api/api-base"; import { ApiBase } from "#api/api-base";
import { SESSION_ID_COOKIE_NAME } from "#app/constants"; import { SESSION_ID_COOKIE_NAME } from "#app/constants";
import type { import type {
AccountChangePwRequest,
AccountInfoResponse, AccountInfoResponse,
AccountLoginRequest, AccountLoginRequest,
AccountLoginResponse, AccountLoginResponse,
@ -95,4 +96,19 @@ export class PokerogueAccountApi extends ApiBase {
removeCookie(SESSION_ID_COOKIE_NAME); // we are always clearing the cookie. removeCookie(SESSION_ID_COOKIE_NAME); // we are always clearing the cookie.
} }
public async changePassword(changePwData: AccountChangePwRequest) {
try {
const response = await this.doPost("/account/changepw", changePwData, "form-urlencoded");
if (response.ok) {
return null;
}
console.warn("Change password failed!", response.status, response.statusText);
return response.text();
} catch (err) {
console.warn("Change password failed!", err);
}
return "Unknown error!";
}
} }

View File

@ -0,0 +1,115 @@
import { globalScene } from "#app/global-scene";
// import type { OptionSelectItem } from "#app/ui/abstact-option-select-ui-handler";
import { pokerogueApi } from "#app/plugins/api/pokerogue-api";
import { UiMode } from "#enums/ui-mode";
import type { InputFieldConfig } from "#ui/form-modal-ui-handler";
import { FormModalUiHandler } from "#ui/form-modal-ui-handler";
import type { ModalConfig } from "#ui/modal-ui-handler";
import i18next from "i18next";
export class ChangePasswordFormUiHandler extends FormModalUiHandler {
private readonly ERR_PASSWORD: string = "invalid password";
private readonly ERR_ACCOUNT_EXIST: string = "account doesn't exist";
private readonly ERR_PASSWORD_MISMATCH: string = "password doesn't match";
constructor(mode: UiMode | null = null) {
super(mode);
}
setup(): void {
super.setup();
}
override getModalTitle(_config?: ModalConfig): string {
return i18next.t("menu:changePassword");
}
override getWidth(_config?: ModalConfig): number {
return 160;
}
override getMargin(_config?: ModalConfig): [number, number, number, number] {
return [0, 0, 48, 0];
}
override getButtonLabels(_config?: ModalConfig): string[] {
return [i18next.t("settings:buttonSubmit"), i18next.t("menu:cancel")];
}
override getReadableErrorMessage(error: string): string {
const colonIndex = error?.indexOf(":");
if (colonIndex > 0) {
error = error.slice(0, colonIndex);
}
switch (error) {
case this.ERR_PASSWORD:
return i18next.t("menu:invalidRegisterPassword");
case this.ERR_ACCOUNT_EXIST:
return i18next.t("menu:accountNonExistent");
case this.ERR_PASSWORD_MISMATCH:
return i18next.t("menu:passwordNotMatchingConfirmPassword");
}
return super.getReadableErrorMessage(error);
}
override getInputFieldConfigs(): InputFieldConfig[] {
const inputFieldConfigs: InputFieldConfig[] = [];
inputFieldConfigs.push({
label: i18next.t("menu:password"),
isPassword: true,
});
inputFieldConfigs.push({
label: i18next.t("menu:confirmPassword"),
isPassword: true,
});
return inputFieldConfigs;
}
override show(args: any[]): boolean {
console.log("=========================1=======================");
if (super.show(args)) {
// Forces the modal to show above the others
// this.modalContainer.parentContainer?.bringToTop(this.modalContainer);
console.log("========================2========================");
const config = args[0] as ModalConfig;
const originalSubmitAction = this.submitAction;
this.submitAction = () => {
if (globalScene.tweens.getTweensOf(this.modalContainer).length === 0) {
// Prevent overlapping overrides on action modification
this.submitAction = originalSubmitAction;
this.sanitizeInputs();
globalScene.ui.setMode(UiMode.LOADING, { buttonActions: [] });
const onFail = (error: string | null) => {
globalScene.ui.setMode(UiMode.CHANGE_PASSWORD_FORM, Object.assign(config, { errorMessage: error?.trim() }));
globalScene.ui.playError();
};
const [passwordInput, confirmPasswordInput] = this.inputs;
if (!passwordInput?.text) {
return onFail(this.getReadableErrorMessage("invalid password"));
}
if (passwordInput.text !== confirmPasswordInput.text) {
return onFail(this.ERR_PASSWORD_MISMATCH);
}
pokerogueApi.account.changePassword({ password: passwordInput.text }).then(error => {
if (!error && originalSubmitAction) {
originalSubmitAction();
} else {
onFail(error);
}
});
}
};
return true;
}
return false;
}
override clear() {
super.clear();
this.setMouseCursorStyle("default"); //reset cursor
}
}

View File

@ -310,6 +310,17 @@ export class MenuUiHandler extends MessageUiHandler {
}, },
keepOpen: true, keepOpen: true,
}, },
{
// Note: i18n key is under `menu`, not `menuUiHandler` to avoid duplication
label: i18next.t("menu:changePassword"),
handler: () => {
ui.setOverlayMode(UiMode.CHANGE_PASSWORD_FORM, {
buttonActions: [() => ui.revertMode(), () => ui.revertMode()],
});
return true;
},
keepOpen: true,
},
{ {
label: i18next.t("menuUiHandler:consentPreferences"), label: i18next.t("menuUiHandler:consentPreferences"),
handler: () => { handler: () => {

View File

@ -12,6 +12,7 @@ import { BallUiHandler } from "#ui/ball-ui-handler";
import { BattleMessageUiHandler } from "#ui/battle-message-ui-handler"; import { BattleMessageUiHandler } from "#ui/battle-message-ui-handler";
import type { BgmBar } from "#ui/bgm-bar"; import type { BgmBar } from "#ui/bgm-bar";
import { GameChallengesUiHandler } from "#ui/challenges-select-ui-handler"; import { GameChallengesUiHandler } from "#ui/challenges-select-ui-handler";
import { ChangePasswordFormUiHandler } from "#ui/change-password-form-ui-handler";
import { CommandUiHandler } from "#ui/command-ui-handler"; import { CommandUiHandler } from "#ui/command-ui-handler";
import { ConfirmUiHandler } from "#ui/confirm-ui-handler"; import { ConfirmUiHandler } from "#ui/confirm-ui-handler";
import { EggGachaUiHandler } from "#ui/egg-gacha-ui-handler"; import { EggGachaUiHandler } from "#ui/egg-gacha-ui-handler";
@ -101,6 +102,7 @@ const noTransitionModes = [
UiMode.ADMIN, UiMode.ADMIN,
UiMode.MYSTERY_ENCOUNTER, UiMode.MYSTERY_ENCOUNTER,
UiMode.RUN_INFO, UiMode.RUN_INFO,
UiMode.CHANGE_PASSWORD_FORM,
]; ];
export class UI extends Phaser.GameObjects.Container { export class UI extends Phaser.GameObjects.Container {
@ -171,6 +173,7 @@ export class UI extends Phaser.GameObjects.Container {
new AutoCompleteUiHandler(), new AutoCompleteUiHandler(),
new AdminUiHandler(), new AdminUiHandler(),
new MysteryEncounterUiHandler(), new MysteryEncounterUiHandler(),
new ChangePasswordFormUiHandler(),
]; ];
} }