Added reauthentication (token freshness) and protected username change

This commit is contained in:
2025-02-15 20:12:30 +11:00
parent 6bee6edd16
commit 42ea74fd63
10 changed files with 315 additions and 14 deletions

View File

@@ -0,0 +1,84 @@
package form
import "fmt"
templ ConfirmPassword(err string) {
{{
xdata := fmt.Sprintf(
"{ errMsg: '%s'}",
err,
)
}}
<form
hx-post="/reauthenticate"
x-data="{ submitted: false, buttontext: 'Confirm' }"
x-on:htmx:xhr:loadstart="submitted=true;buttontext='Loading...'"
>
<div
class="grid gap-y-4"
x-data={ xdata }
>
<div class="mt-5">
<div class="relative">
<input
type="password"
id="password"
name="password"
class="py-3 px-4 block w-full rounded-lg text-sm
focus:border-blue focus:ring-blue bg-base
disabled:opacity-50 disabled:pointer-events-none"
placeholder="Confirm password"
required
aria-describedby="password-error"
/>
<div
class="absolute inset-y-0 end-0
pointer-events-none pe-3 pt-3"
x-show="errMsg"
x-cloak
>
<svg
class="size-5 text-red"
width="16"
height="16"
fill="currentColor"
viewBox="0 0 16 16"
aria-hidden="true"
>
<path
d="M16 8A8 8 0 1 1 0 8a8 8 0 0 1 16 0zM8
4a.905.905 0 0 0-.9.995l.35 3.507a.552.552 0 0
0 1.1 0l.35-3.507A.905.905 0 0 0 8 4zm.002 6a1
1 0 1 0 0 2 1 1 0 0 0 0-2z"
></path>
</svg>
</div>
</div>
<p
class="text-center text-xs text-red mt-2"
id="password-error"
x-show="errMsg"
x-cloak
x-text="errMsg"
></p>
</div>
<button
x-bind:disabled="submitted"
x-text="buttontext"
type="submit"
class="w-full py-3 px-4 inline-flex justify-center items-center
gap-x-2 rounded-lg border border-transparent transition
bg-blue hover:bg-blue/75 text-mantle hover:cursor-pointer
disabled:bg-blue/60 disabled:cursor-default"
></button>
<button
type="button"
class="w-full py-3 px-4 inline-flex justify-center items-center
gap-x-2 rounded-lg border border-transparent transition
bg-surface2 hover:bg-surface1 hover:cursor-pointer
disabled:cursor-default"
@click="showConfirmPasswordModal=false"
>Cancel</button>
</div>
</form>
}

View File

@@ -0,0 +1,20 @@
package popup
import "projectreshoot/view/component/form"
templ ConfirmPasswordModal() {
<div
class="z-50 absolute bg-overlay0/55 top-0 left-0 right-0 bottom-0"
x-show="showConfirmPasswordModal"
>
<div
class="p-5 mt-25 w-fit max-w-100 text-center rounded-lg bg-mantle mx-auto"
>
<div class="text-xl">
To complete this action you need to confirm your password
</div>
@form.ConfirmPassword("")
</div>
</div>
}

View File

@@ -1,4 +1,4 @@
package component
package popup
templ ErrorPopup() {
<div

View File

@@ -2,7 +2,7 @@ package layout
import "projectreshoot/view/component/nav"
import "projectreshoot/view/component/footer"
import "projectreshoot/view/component"
import "projectreshoot/view/component/popup"
// Global page layout. Includes HTML document settings, header tags
// navbar and footer
@@ -40,16 +40,50 @@ templ Global() {
<script src="https://unpkg.com/alpinejs" defer></script>
<script>
// uncomment this line to enable logging of htmx events
//htmx.logAll();
// htmx.logAll();
</script>
<script>
const popups = {
showError: false,
showConfirmPasswordModal: false,
handleHtmxBeforeOnLoad(event) {
const requestPath = event.detail.pathInfo.requestPath;
if (requestPath === "/reauthenticate") {
// handle password incorrect on refresh attempt
if (event.detail.xhr.status === 445) {
event.detail.shouldSwap = true;
event.detail.isError = false;
} else if (event.detail.xhr.status === 200) {
this.showConfirmPasswordModal = false;
}
}
},
// handle errors from the server on HTMX requests
handleHtmxError(event) {
const errorCode = event.detail.errorInfo.error;
// internal server error
if (errorCode.includes('Code 500')) {
this.showError = true;
setTimeout(() => this.showError = false, 6000);
}
// user is authorized but needs to refresh their login
if (errorCode.includes('Code 444')) {
this.showConfirmPasswordModal = true;
}
},
};
</script>
</head>
<body
class="bg-base text-text ubuntu-mono-regular overflow-x-hidden"
x-data="{ showError: false }"
x-on:htmx:error="if ($event.detail.errorInfo.error.includes('Code 500'))
showError = true; setTimeout(() => showError = false, 6000)"
x-data="popups"
x-on:htmx:error="handleHtmxError($event)"
x-on:htmx:before-on-load="handleHtmxBeforeOnLoad($event)"
>
@component.ErrorPopup()
@popup.ErrorPopup()
@popup.ConfirmPasswordModal()
<div
id="main-content"
class="flex flex-col h-screen justify-between"