migrate register account to pokerogue-api

This commit is contained in:
flx-sta 2024-10-04 11:55:54 -07:00
parent 9b18070506
commit 13b4b0e83d
3 changed files with 36 additions and 17 deletions

View File

@ -0,0 +1,4 @@
export interface AccountRegisterRequest {
username: string;
password: string;
}

View File

@ -13,6 +13,7 @@ import type { SessionSaveData } from "#app/system/game-data";
import type { RankingEntry, ScoreboardCategory } from "#app/ui/daily-run-scoreboard"; import type { RankingEntry, ScoreboardCategory } from "#app/ui/daily-run-scoreboard";
import { removeCookie, setCookie } from "#app/utils"; import { removeCookie, setCookie } from "#app/utils";
import { PokerogueAdminApi } from "#app/plugins/api/pokerogue-admin-api"; import { PokerogueAdminApi } from "#app/plugins/api/pokerogue-admin-api";
import type { AccountRegisterRequest } from "./models/AccountRegister";
export class PokerogueApi extends Api { export class PokerogueApi extends Api {
//#region Fields //#region Fields
@ -84,6 +85,27 @@ export class PokerogueApi extends Api {
return "Unknown error!"; 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. * Send a logout request.
* **Always** (no matter if failed or not) removes the session cookie. * **Always** (no matter if failed or not) removes the session cookie.

View File

@ -1,9 +1,9 @@
import { FormModalUiHandler } from "./form-modal-ui-handler"; import { FormModalUiHandler } from "./form-modal-ui-handler";
import { ModalConfig } from "./modal-ui-handler"; import { ModalConfig } from "./modal-ui-handler";
import * as Utils from "../utils";
import { Mode } from "./ui"; import { Mode } from "./ui";
import { TextStyle, addTextObject } from "./text"; import { TextStyle, addTextObject } from "./text";
import i18next from "i18next"; import i18next from "i18next";
import { pokerogueApi } from "#app/plugins/api/pokerogue-api";
interface LanguageSetting { interface LanguageSetting {
@ -106,27 +106,20 @@ export default class RegistrationFormUiHandler extends FormModalUiHandler {
if (this.inputs[1].text !== this.inputs[2].text) { if (this.inputs[1].text !== this.inputs[2].text) {
return onFail(i18next.t("menu:passwordNotMatchingConfirmPassword")); 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") const [usernameInput, passwordInput] = this.inputs;
.then(response => response.text()) pokerogueApi.register({ username: usernameInput.text, password: passwordInput.text })
.then(response => { .then(registerError => {
if (!response) { if (!registerError) {
Utils.apiPost("account/login", `username=${encodeURIComponent(this.inputs[0].text)}&password=${encodeURIComponent(this.inputs[1].text)}`, "application/x-www-form-urlencoded") pokerogueApi.login({ username: usernameInput.text, password: passwordInput.text })
.then(response => { .then(loginError => {
if (!response.ok) { if (!loginError) {
return response.text();
}
return response.json();
})
.then(response => {
if (response.hasOwnProperty("token")) {
Utils.setCookie(Utils.sessionIdKey, response.token);
originalRegistrationAction && originalRegistrationAction(); originalRegistrationAction && originalRegistrationAction();
} else { } else {
onFail(response); onFail(loginError);
} }
}); });
} else { } else {
onFail(response); onFail(registerError);
} }
}); });
}; };