From b5dea7755b5d801528e244e11a906d64b2c6c2a2 Mon Sep 17 00:00:00 2001 From: Harvey Tindall Date: Tue, 27 Aug 2024 14:56:24 +0100 Subject: [PATCH] userpage: add password reset direct link for #363, adds /my/account/password/reset. Navigate to it to skip pressing the "forgot password?" button on the login screen. Works with the nice-ish onpopstate override thing I put in setup.ts a while ago. Maybe I should make it a module. --- router.go | 1 + ts/modules/login.ts | 2 ++ ts/setup.ts | 1 - ts/typings/d.ts | 2 +- ts/user.ts | 36 +++++++++++++++++++++++++++++------- 5 files changed, 33 insertions(+), 9 deletions(-) diff --git a/router.go b/router.go index 6316214..9edc32c 100644 --- a/router.go +++ b/router.go @@ -159,6 +159,7 @@ func (app *appContext) loadRoutes(router *gin.Engine) { } if userPageEnabled { router.GET(p+"/my/account", app.MyUserPage) + router.GET(p+"/my/account/password/reset", app.MyUserPage) router.GET(p+"/my/token/login", app.getUserTokenLogin) router.GET(p+"/my/token/refresh", app.getUserTokenRefresh) router.GET(p+"/my/confirm/:jwt", app.ConfirmMyAction) diff --git a/ts/modules/login.ts b/ts/modules/login.ts index 12e6aba..ea74103 100644 --- a/ts/modules/login.ts +++ b/ts/modules/login.ts @@ -2,6 +2,7 @@ import { Modal } from "../modules/modal.js"; import { toggleLoader, _post } from "../modules/common.js"; export class Login { + loggedIn: boolean = false; private _modal: Modal; private _form: HTMLFormElement; private _url: string; @@ -91,6 +92,7 @@ export class Login { } else { const data = req.response; window.token = data["token"]; + this.loggedIn = true; if (this._onLogin) { this._onLogin(username, password); } diff --git a/ts/setup.ts b/ts/setup.ts index 3b10e65..0fb2f6f 100644 --- a/ts/setup.ts +++ b/ts/setup.ts @@ -525,7 +525,6 @@ const pageNames: string[][] = []; })(); window.onpopstate = (event: PopStateEvent) => { - console.log("CALLLLLLLL", event); if (event.state === "welcome") { cards[0].classList.remove("unfocused"); for (let i = 1; i < cards.length; i++) { cards[i].classList.add("unfocused"); } diff --git a/ts/typings/d.ts b/ts/typings/d.ts index 34beb70..bcba96d 100644 --- a/ts/typings/d.ts +++ b/ts/typings/d.ts @@ -2,7 +2,7 @@ declare interface Modal { modal: HTMLElement; closeButton: HTMLSpanElement show: () => void; - close: (event?: Event) => void; + close: (event?: Event, noDispatch?: boolean) => void; toggle: () => void; onopen: (f: () => void) => void; onclose: (f: () => void) => void; diff --git a/ts/user.ts b/ts/user.ts index 44bfac3..937a778 100644 --- a/ts/user.ts +++ b/ts/user.ts @@ -50,19 +50,37 @@ window.modals = {} as Modals; if (window.pwrEnabled) { window.modals.pwr = new Modal(document.getElementById("modal-pwr"), false); window.modals.pwr.onclose = () => { - window.modals.login.show(); + window.history.pushState("", "", window.location.pathname.replace("/password/reset", "")); }; const resetButton = document.getElementById("modal-login-pwr"); resetButton.onclick = () => { - const usernameInput = document.getElementById("login-user") as HTMLInputElement; - const input = document.getElementById("pwr-address") as HTMLInputElement; - input.value = usernameInput.value; - window.modals.login.close(); - window.modals.pwr.show(); + window.history.pushState("reset", "", window.location.pathname+"/password/reset"); } + + window.onpopstate = (event: PopStateEvent) => { + if ((event.state == "reset" || window.location.pathname.includes("/password/reset")) && window.pwrEnabled) { + const usernameInput = document.getElementById("login-user") as HTMLInputElement; + const input = document.getElementById("pwr-address") as HTMLInputElement; + input.value = usernameInput.value; + window.modals.login.close(); + window.modals.pwr.show(); + } else { + window.modals.pwr.close(null, true); + if (!login.loggedIn) login.login("", ""); + } + }; } })(); +(() => { + const pushState = window.history.pushState; + window.history.pushState = function (data: any, __: string, _: string | URL) { + pushState.apply(window.history, arguments); + let ev = { state: data as string } as PopStateEvent; + window.onpopstate(ev); + }; +})(); + window.notifications = new notificationBox(document.getElementById('notification-box') as HTMLDivElement, 5); if (window.pwrEnabled && window.linkResetEnabled) { @@ -780,4 +798,8 @@ const generatePermutations = (xs: number[]): [number[], number[]][] => { login.bindLogout(document.getElementById("logout-button")); -login.login("", ""); +(() => { + let data = ""; + if (window.location.pathname.endsWith("/password/reset")) data = "reset"; + window.history.pushState(data, "", window.location.pathname); +})();