From 13b4b0e83d56ea48c4510bd902ab83e8294fe436 Mon Sep 17 00:00:00 2001 From: flx-sta <50131232+flx-sta@users.noreply.github.com> Date: Fri, 4 Oct 2024 11:55:54 -0700 Subject: [PATCH] migrate register account to pokerogue-api --- src/plugins/api/models/AccountRegister.ts | 4 ++++ src/plugins/api/pokerogue-api.ts | 22 ++++++++++++++++++ src/ui/registration-form-ui-handler.ts | 27 +++++++++-------------- 3 files changed, 36 insertions(+), 17 deletions(-) create mode 100644 src/plugins/api/models/AccountRegister.ts diff --git a/src/plugins/api/models/AccountRegister.ts b/src/plugins/api/models/AccountRegister.ts new file mode 100644 index 00000000000..b06d77cc284 --- /dev/null +++ b/src/plugins/api/models/AccountRegister.ts @@ -0,0 +1,4 @@ +export interface AccountRegisterRequest { + username: string; + password: string; +} diff --git a/src/plugins/api/pokerogue-api.ts b/src/plugins/api/pokerogue-api.ts index 0e4e9b62894..b001c9d948e 100644 --- a/src/plugins/api/pokerogue-api.ts +++ b/src/plugins/api/pokerogue-api.ts @@ -13,6 +13,7 @@ import type { SessionSaveData } from "#app/system/game-data"; import type { RankingEntry, ScoreboardCategory } from "#app/ui/daily-run-scoreboard"; import { removeCookie, setCookie } from "#app/utils"; import { PokerogueAdminApi } from "#app/plugins/api/pokerogue-admin-api"; +import type { AccountRegisterRequest } from "./models/AccountRegister"; export class PokerogueApi extends Api { //#region Fields @@ -84,6 +85,27 @@ export class PokerogueApi extends Api { return "Unknown error!"; } + /** + * Register a new account. + * @param registerData The {@linkcode AccountRegisterRequest} to send + * @returns An error message if something went wrong + */ + public async register(registerData: AccountRegisterRequest) { + try { + const response = await this.doPost("/account/register", registerData, "form-urlencoded"); + + if (response.ok) { + return null; + } else { + return response.text(); + } + } catch (err) { + console.warn("Register failed!", err); + } + + return "Unknown error!"; + } + /** * Send a logout request. * **Always** (no matter if failed or not) removes the session cookie. diff --git a/src/ui/registration-form-ui-handler.ts b/src/ui/registration-form-ui-handler.ts index 0c4b54ac723..f45b71b50ba 100644 --- a/src/ui/registration-form-ui-handler.ts +++ b/src/ui/registration-form-ui-handler.ts @@ -1,9 +1,9 @@ import { FormModalUiHandler } from "./form-modal-ui-handler"; import { ModalConfig } from "./modal-ui-handler"; -import * as Utils from "../utils"; import { Mode } from "./ui"; import { TextStyle, addTextObject } from "./text"; import i18next from "i18next"; +import { pokerogueApi } from "#app/plugins/api/pokerogue-api"; interface LanguageSetting { @@ -106,27 +106,20 @@ export default class RegistrationFormUiHandler extends FormModalUiHandler { if (this.inputs[1].text !== this.inputs[2].text) { return onFail(i18next.t("menu:passwordNotMatchingConfirmPassword")); } - Utils.apiPost("account/register", `username=${encodeURIComponent(this.inputs[0].text)}&password=${encodeURIComponent(this.inputs[1].text)}`, "application/x-www-form-urlencoded") - .then(response => response.text()) - .then(response => { - if (!response) { - Utils.apiPost("account/login", `username=${encodeURIComponent(this.inputs[0].text)}&password=${encodeURIComponent(this.inputs[1].text)}`, "application/x-www-form-urlencoded") - .then(response => { - if (!response.ok) { - return response.text(); - } - return response.json(); - }) - .then(response => { - if (response.hasOwnProperty("token")) { - Utils.setCookie(Utils.sessionIdKey, response.token); + const [usernameInput, passwordInput] = this.inputs; + pokerogueApi.register({ username: usernameInput.text, password: passwordInput.text }) + .then(registerError => { + if (!registerError) { + pokerogueApi.login({ username: usernameInput.text, password: passwordInput.text }) + .then(loginError => { + if (!loginError) { originalRegistrationAction && originalRegistrationAction(); } else { - onFail(response); + onFail(loginError); } }); } else { - onFail(response); + onFail(registerError); } }); };