This commit is contained in:
Elijah R 2025-01-06 23:09:04 -05:00
parent 3221e2c86f
commit 7c67d8c57c
2 changed files with 31 additions and 31 deletions

View file

@ -21,9 +21,9 @@ export default class AuthManager {
login(username : string, password : string, captchaToken : string | undefined, turnstileToken : string | undefined, recaptchaToken : string | undefined) : Promise<AccountLoginResult> { login(username : string, password : string, captchaToken : string | undefined, turnstileToken : string | undefined, recaptchaToken : string | undefined) : Promise<AccountLoginResult> {
return new Promise(async (res,rej) => { return new Promise(async (res,rej) => {
if (!this.info) throw new Error("Cannot login before fetching API information."); if (!this.info) throw new Error("Cannot login before fetching API information.");
if (!captchaToken && this.info.hcaptcha.required) throw new Error("This API requires a valid hCaptcha token."); if (!captchaToken && this.info.hcaptcha?.required) throw new Error("This API requires a valid hCaptcha token.");
if (!turnstileToken && this.info.turnstile.required) throw new Error("This API requires a valid Turnstile token."); if (!turnstileToken && this.info.turnstile?.required) throw new Error("This API requires a valid Turnstile token.");
if (!recaptchaToken && this.info.recaptcha.required) throw new Error("This API requires a valid reCAPTCHA token."); if (!recaptchaToken && this.info.recaptcha?.required) throw new Error("This API requires a valid reCAPTCHA token.");
var data = await fetch(this.apiEndpoint + "/api/v1/login", { var data = await fetch(this.apiEndpoint + "/api/v1/login", {
method: "POST", method: "POST",
headers: { headers: {
@ -76,9 +76,9 @@ export default class AuthManager {
register(username : string, password : string, email : string, dateOfBirth : dayjs.Dayjs, captchaToken : string | undefined, turnstileToken: string | undefined, recaptchaToken : string | undefined) : Promise<AccountRegisterResult> { register(username : string, password : string, email : string, dateOfBirth : dayjs.Dayjs, captchaToken : string | undefined, turnstileToken: string | undefined, recaptchaToken : string | undefined) : Promise<AccountRegisterResult> {
return new Promise(async (res, rej) => { return new Promise(async (res, rej) => {
if (!this.info) throw new Error("Cannot login before fetching API information."); if (!this.info) throw new Error("Cannot login before fetching API information.");
if (!captchaToken && this.info.hcaptcha.required) throw new Error("This API requires a valid hCaptcha token."); if (!captchaToken && this.info.hcaptcha?.required) throw new Error("This API requires a valid hCaptcha token.");
if (!turnstileToken && this.info.turnstile.required) throw new Error("This API requires a valid Turnstile token."); if (!turnstileToken && this.info.turnstile?.required) throw new Error("This API requires a valid Turnstile token.");
if (!recaptchaToken && this.info.recaptcha.required) throw new Error("This API requires a valid reCAPTCHA token."); if (!recaptchaToken && this.info.recaptcha?.required) throw new Error("This API requires a valid reCAPTCHA token.");
var data = await fetch(this.apiEndpoint + "/api/v1/register", { var data = await fetch(this.apiEndpoint + "/api/v1/register", {
method: "POST", method: "POST",
headers: { headers: {
@ -165,9 +165,9 @@ export default class AuthManager {
sendPasswordResetEmail(username : string, email : string, captchaToken : string | undefined, turnstileToken : string | undefined, recaptchaToken : string | undefined) { sendPasswordResetEmail(username : string, email : string, captchaToken : string | undefined, turnstileToken : string | undefined, recaptchaToken : string | undefined) {
return new Promise<PasswordResetResult>(async res => { return new Promise<PasswordResetResult>(async res => {
if (!this.info) throw new Error("Cannot send password reset email without fetching API information."); if (!this.info) throw new Error("Cannot send password reset email without fetching API information.");
if (!captchaToken && this.info.hcaptcha.required) throw new Error("This API requires a valid hCaptcha token."); if (!captchaToken && this.info.hcaptcha?.required) throw new Error("This API requires a valid hCaptcha token.");
if (!turnstileToken && this.info.turnstile.required) throw new Error("This API requires a valid Turnstile token."); if (!turnstileToken && this.info.turnstile?.required) throw new Error("This API requires a valid Turnstile token.");
if (!recaptchaToken && this.info.recaptcha.required) throw new Error("This API requires a valid reCAPTCHA token."); if (!recaptchaToken && this.info.recaptcha?.required) throw new Error("This API requires a valid reCAPTCHA token.");
var data = await fetch(this.apiEndpoint + "/api/v1/sendreset", { var data = await fetch(this.apiEndpoint + "/api/v1/sendreset", {
method: "POST", method: "POST",
headers: { headers: {

View file

@ -964,7 +964,7 @@ async function renderAuth() {
element.remove(); element.remove();
} }
if (auth!.info!.hcaptcha.required) { if (auth!.info!.hcaptcha?.required) {
const hconfig = { sitekey: auth!.info!.hcaptcha.siteKey! }; const hconfig = { sitekey: auth!.info!.hcaptcha.siteKey! };
let renderHcaptcha = () => { let renderHcaptcha = () => {
@ -1001,7 +1001,7 @@ async function renderAuth() {
} }
} }
if (auth!.info?.turnstile.required) { if (auth!.info?.turnstile?.required) {
const turnstileConfig = { sitekey: auth!.info!.turnstile.siteKey! }; const turnstileConfig = { sitekey: auth!.info!.turnstile.siteKey! };
let renderTurnstile = () => { let renderTurnstile = () => {
@ -1038,7 +1038,7 @@ async function renderAuth() {
} }
} }
if (auth!.info?.recaptcha.required) { if (auth!.info?.recaptcha?.required) {
const recaptchaConfig = { sitekey: auth!.info!.recaptcha.siteKey! }; const recaptchaConfig = { sitekey: auth!.info!.recaptcha.siteKey! };
let renderRecaptcha = () => { let renderRecaptcha = () => {
@ -1148,7 +1148,7 @@ elements.accountLoginForm.addEventListener('submit', async (e) => {
e.preventDefault(); e.preventDefault();
var hcaptchaToken = undefined; var hcaptchaToken = undefined;
var hcaptchaID = undefined; var hcaptchaID = undefined;
if (auth!.info!.hcaptcha.required) { if (auth!.info!.hcaptcha?.required) {
hcaptchaID = elements.accountLoginCaptchaContainer.getAttribute("data-hcaptcha-widget-id")! hcaptchaID = elements.accountLoginCaptchaContainer.getAttribute("data-hcaptcha-widget-id")!
var response = hcaptcha.getResponse(hcaptchaID); var response = hcaptcha.getResponse(hcaptchaID);
if (response === "") { if (response === "") {
@ -1162,7 +1162,7 @@ elements.accountLoginForm.addEventListener('submit', async (e) => {
var turnstileToken = undefined; var turnstileToken = undefined;
var turnstileID = undefined; var turnstileID = undefined;
if (auth!.info!.turnstile.required) { if (auth!.info!.turnstile?.required) {
turnstileID = elements.accountLoginTurnstileContainer.getAttribute("data-turnstile-widget-id")! turnstileID = elements.accountLoginTurnstileContainer.getAttribute("data-turnstile-widget-id")!
var response: string = turnstile.getResponse(turnstileID) || ""; var response: string = turnstile.getResponse(turnstileID) || "";
if (response === "") { if (response === "") {
@ -1176,7 +1176,7 @@ elements.accountLoginForm.addEventListener('submit', async (e) => {
var recaptchaToken = undefined; var recaptchaToken = undefined;
var recaptchaID = undefined; var recaptchaID = undefined;
if (auth!.info!.recaptcha.required) { if (auth!.info!.recaptcha?.required) {
recaptchaID = parseInt(elements.accountLoginRecaptchaContainer.getAttribute("data-recaptcha-widget-id")!) recaptchaID = parseInt(elements.accountLoginRecaptchaContainer.getAttribute("data-recaptcha-widget-id")!)
var response = grecaptcha.getResponse(recaptchaID); var response = grecaptcha.getResponse(recaptchaID);
if (response === "") { if (response === "") {
@ -1190,9 +1190,9 @@ elements.accountLoginForm.addEventListener('submit', async (e) => {
var username = elements.accountLoginUsername.value; var username = elements.accountLoginUsername.value;
var password = elements.accountLoginPassword.value; var password = elements.accountLoginPassword.value;
var result = await auth!.login(username, password, hcaptchaToken, turnstileToken, recaptchaToken); var result = await auth!.login(username, password, hcaptchaToken, turnstileToken, recaptchaToken);
if (auth!.info!.hcaptcha.required) hcaptcha.reset(hcaptchaID); if (auth!.info!.hcaptcha?.required) hcaptcha.reset(hcaptchaID);
if (auth!.info!.turnstile.required) turnstile.reset(turnstileID); if (auth!.info!.turnstile?.required) turnstile.reset(turnstileID);
if (auth!.info!.recaptcha.required) grecaptcha.reset(recaptchaID); if (auth!.info!.recaptcha?.required) grecaptcha.reset(recaptchaID);
if (result.success) { if (result.success) {
elements.accountLoginUsername.value = ""; elements.accountLoginUsername.value = "";
elements.accountLoginPassword.value = ""; elements.accountLoginPassword.value = "";
@ -1216,7 +1216,7 @@ elements.accountRegisterForm.addEventListener('submit', async (e) => {
e.preventDefault(); e.preventDefault();
var hcaptchaToken = undefined; var hcaptchaToken = undefined;
var hcaptchaID = undefined; var hcaptchaID = undefined;
if (auth!.info!.hcaptcha.required) { if (auth!.info!.hcaptcha?.required) {
hcaptchaID = elements.accountRegisterCaptchaContainer.getAttribute("data-hcaptcha-widget-id")! hcaptchaID = elements.accountRegisterCaptchaContainer.getAttribute("data-hcaptcha-widget-id")!
var response = hcaptcha.getResponse(hcaptchaID); var response = hcaptcha.getResponse(hcaptchaID);
if (response === "") { if (response === "") {
@ -1230,7 +1230,7 @@ elements.accountRegisterForm.addEventListener('submit', async (e) => {
var turnstileToken = undefined; var turnstileToken = undefined;
var turnstileID = undefined; var turnstileID = undefined;
if (auth!.info!.turnstile.required) { if (auth!.info!.turnstile?.required) {
turnstileID = elements.accountRegisterTurnstileContainer.getAttribute("data-turnstile-widget-id")! turnstileID = elements.accountRegisterTurnstileContainer.getAttribute("data-turnstile-widget-id")!
var response: string = turnstile.getResponse(turnstileID) || ""; var response: string = turnstile.getResponse(turnstileID) || "";
if (response === "") { if (response === "") {
@ -1244,7 +1244,7 @@ elements.accountRegisterForm.addEventListener('submit', async (e) => {
var recaptchaToken = undefined; var recaptchaToken = undefined;
var recaptchaID = undefined; var recaptchaID = undefined;
if (auth!.info!.recaptcha.required) { if (auth!.info!.recaptcha?.required) {
recaptchaID = parseInt(elements.accountRegisterRecaptchaContainer.getAttribute("data-recaptcha-widget-id")!) recaptchaID = parseInt(elements.accountRegisterRecaptchaContainer.getAttribute("data-recaptcha-widget-id")!)
var response = grecaptcha.getResponse(recaptchaID); var response = grecaptcha.getResponse(recaptchaID);
if (response === "") { if (response === "") {
@ -1265,9 +1265,9 @@ elements.accountRegisterForm.addEventListener('submit', async (e) => {
return false; return false;
} }
var result = await auth!.register(username, password, email, dob, hcaptchaToken, turnstileToken, recaptchaToken); var result = await auth!.register(username, password, email, dob, hcaptchaToken, turnstileToken, recaptchaToken);
if (auth!.info!.hcaptcha.required) hcaptcha.reset(hcaptchaID); if (auth!.info!.hcaptcha?.required) hcaptcha.reset(hcaptchaID);
if (auth!.info!.turnstile.required) turnstile.reset(turnstileID); if (auth!.info!.turnstile?.required) turnstile.reset(turnstileID);
if (auth!.info!.recaptcha.required) grecaptcha.reset(recaptchaID); if (auth!.info!.recaptcha?.required) grecaptcha.reset(recaptchaID);
if (result.success) { if (result.success) {
elements.accountRegisterUsername.value = ""; elements.accountRegisterUsername.value = "";
elements.accountRegisterEmail.value = ""; elements.accountRegisterEmail.value = "";
@ -1360,7 +1360,7 @@ elements.accountResetPasswordForm.addEventListener('submit', async e => {
e.preventDefault(); e.preventDefault();
var hcaptchaToken = undefined; var hcaptchaToken = undefined;
var hcaptchaID = undefined; var hcaptchaID = undefined;
if (auth!.info!.hcaptcha.required) { if (auth!.info!.hcaptcha?.required) {
hcaptchaID = elements.accountResetPasswordCaptchaContainer.getAttribute("data-hcaptcha-widget-id")! hcaptchaID = elements.accountResetPasswordCaptchaContainer.getAttribute("data-hcaptcha-widget-id")!
var response = hcaptcha.getResponse(hcaptchaID); var response = hcaptcha.getResponse(hcaptchaID);
if (response === "") { if (response === "") {
@ -1374,7 +1374,7 @@ elements.accountResetPasswordForm.addEventListener('submit', async e => {
var turnstileToken = undefined; var turnstileToken = undefined;
var turnstileID = undefined; var turnstileID = undefined;
if (auth!.info!.turnstile.required) { if (auth!.info!.turnstile?.required) {
turnstileID = elements.accountResetPasswordTurnstileContainer.getAttribute("data-turnstile-widget-id")! turnstileID = elements.accountResetPasswordTurnstileContainer.getAttribute("data-turnstile-widget-id")!
var response: string = turnstile.getResponse(turnstileID) || ""; var response: string = turnstile.getResponse(turnstileID) || "";
if (response === "") { if (response === "") {
@ -1388,7 +1388,7 @@ elements.accountResetPasswordForm.addEventListener('submit', async e => {
var recaptchaToken = undefined; var recaptchaToken = undefined;
var recaptchaID = undefined; var recaptchaID = undefined;
if (auth!.info!.recaptcha.required) { if (auth!.info!.recaptcha?.required) {
recaptchaID = parseInt(elements.accountResetPasswordRecaptchaContainer.getAttribute("data-recaptcha-widget-id")!) recaptchaID = parseInt(elements.accountResetPasswordRecaptchaContainer.getAttribute("data-recaptcha-widget-id")!)
var response = grecaptcha.getResponse(recaptchaID); var response = grecaptcha.getResponse(recaptchaID);
if (response === "") { if (response === "") {
@ -1402,9 +1402,9 @@ elements.accountResetPasswordForm.addEventListener('submit', async e => {
var username = elements.accountResetPasswordUsername.value; var username = elements.accountResetPasswordUsername.value;
var email = elements.accountResetPasswordEmail.value; var email = elements.accountResetPasswordEmail.value;
var result = await auth!.sendPasswordResetEmail(username, email, hcaptchaToken, turnstileToken, recaptchaToken); var result = await auth!.sendPasswordResetEmail(username, email, hcaptchaToken, turnstileToken, recaptchaToken);
if (auth!.info!.hcaptcha.required) hcaptcha.reset(hcaptchaID); if (auth!.info!.hcaptcha?.required) hcaptcha.reset(hcaptchaID);
if (auth!.info!.turnstile.required) turnstile.reset(turnstileID); if (auth!.info!.turnstile?.required) turnstile.reset(turnstileID);
if (auth!.info!.recaptcha.required) grecaptcha.reset(recaptchaID); if (auth!.info!.recaptcha?.required) grecaptcha.reset(recaptchaID);
if (result.success) { if (result.success) {
resetPasswordUsername = username; resetPasswordUsername = username;
resetPasswordEmail = email; resetPasswordEmail = email;