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 { 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.

View File

@ -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);
}
});
};