mirror of
https://github.com/pagefaultgames/pokerogue.git
synced 2025-02-27 06:28:30 +00:00
migrate register account to pokerogue-api
This commit is contained in:
parent
9b18070506
commit
13b4b0e83d
4
src/plugins/api/models/AccountRegister.ts
Normal file
4
src/plugins/api/models/AccountRegister.ts
Normal file
@ -0,0 +1,4 @@
|
||||
export interface AccountRegisterRequest {
|
||||
username: string;
|
||||
password: string;
|
||||
}
|
@ -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.
|
||||
|
@ -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);
|
||||
}
|
||||
});
|
||||
};
|
||||
|
Loading…
x
Reference in New Issue
Block a user