Better login flow

This commit is contained in:
2024-05-24 11:15:17 +01:00
parent 92f1ba6529
commit a76ee50664
6 changed files with 74 additions and 39 deletions
+57 -31
View File
@@ -1,10 +1,5 @@
<div class="center">
<form
id="fax-form"
action="submit"
hx-post="/send-fax"
hx-swap="innerHTML"
hx-target="#req-result">
<form id="fax-form" action="submit" hx-post="/send-fax" hx-swap="innerHTML" hx-target="#req-result">
<fieldset id="fax-message-boundaries">
<legend>FAX</legend>
@@ -20,11 +15,48 @@
</div>
<script>
// This function will persist the current editor data
// and send the user back to the login page
function reauth() {
localStorage.setItem("editor-text", document.getElementById("fax-message").value)
window.location.replace(window.location.origin + "/")
}
// this will load the editor data if we have it in storage
function loadStorageData() {
const savedContent = localStorage.getItem('editor-text');
if (savedContent) {
document.getElementById('fax-message').value = savedContent;
}
}
// If the fax is sent successfully we no longer need to cache it
// We will persist in the DB
function resetStorageData() {
localStorage.setItem("editor-text", document.getElementById("fax-message").value)
}
function handleSuccessfulPOST(data) {
console.log(data)
const response = JSON.parse(data)
var elem = document.getElementById("req-result")
if (response["state"] === "success") {
elem.innerHTML = "-> Fax sent successfully <-"
resetStorageData();
} else if (response["state"] === "unauthenticated") {
reauth();
} else {
elem.innerHTML = "-> Fax failed to send <-"
}
}
// Get a reference to the submit button
var submitBtn = document.getElementById("fax-submit-button");
// Add a click event listener to the submit button
submitBtn.addEventListener("click", function() {
submitBtn.addEventListener("click", function () {
// Get the fax message from the textarea
var faxMessage = document.getElementById("fax-message").value;
@@ -41,29 +73,23 @@
},
body: JSON.stringify(jsonData)
})
.then(response => {
// Check if the response is OK
if (response.ok) {
// Request was successful, handle response if needed
return response.text();
} else {
// Request failed, handle error if needed
throw new Error("Error: " + response.statusText);
}
})
.then(data => {
const response = JSON.parse(data)
console.log(data)
var elem = document.getElementById("req-result")
if (response["state"] === "success") {
elem.innerHTML = "-> Fax sent successfully <-"
} else {
elem.innerHTML = "-> Fax failed to send <-"
}
})
.catch(error => {
console.error(error);
});
.then(response => {
// Check if the response is OK
if (response.ok) {
// Request was successful, handle response if needed
return response.text();
} else {
// Request failed, handle error if needed
throw new Error("Error: " + response.statusText);
}
})
.then(data => {
handleSuccessfulPOST(data)
})
.catch(error => {
console.error(error);
});
});
loadStorageData();
</script>
+4 -3
View File
@@ -17,6 +17,7 @@
</form>
</div>
{{ if .script }}
<script>
// Get a reference to the submit button
var submitBtn = document.getElementById("login-submit-button");
@@ -52,11 +53,11 @@
}
})
.then(data => {
window.location.replace("http://localhost:8083/")
console.log("Success:", data);
window.location.replace(window.location.origin + "/")
})
.catch(error => {
console.error(error);
});
});
</script>
</script>
{{ end }}
+4
View File
@@ -63,6 +63,9 @@ func sendRequestToPrinter(msg string) (PrintServerResponse, error) {
func SendFax(w http.ResponseWriter, r *http.Request) {
authd, token := isAuthenticated(r)
if !authd {
w.Header().Set("Content-Type", "text/json")
w.Write([]byte("{\"state\":\"unauthenticated\"}"))
return
}
@@ -82,6 +85,7 @@ func SendFax(w http.ResponseWriter, r *http.Request) {
_, err = sendRequestToPrinter(formData.Message)
if err != nil {
log.Println("Printer failed to process fax:", err)
w.Write([]byte("{\"state\":\"failed\"}"))
return
}
+2 -2
View File
@@ -5,8 +5,8 @@ import "net/http"
func EditorPage(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
mainPage := htmlTemplate("./assets/htmx/terminal.tmpl", map[string]interface{}{
"content": htmlTemplate("./assets/htmx/editor.html", nil),
mainPage := HtmlTemplate("./assets/htmx/terminal.tmpl", map[string]interface{}{
"content": HtmlTemplate("./assets/htmx/editor.html", nil),
"renderNavBar": true,
"page": "fax",
})
+6 -2
View File
@@ -5,8 +5,12 @@ import "net/http"
func LoginPage(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
loginPage := htmlTemplate("./assets/htmx/terminal.tmpl", map[string]interface{}{
"content": htmlTemplate("./assets/htmx/login.html", nil),
loginTemplate := HtmlTemplate("./assets/htmx/login.html", map[string]interface{}{
"script": true,
})
loginPage := HtmlTemplate("./assets/htmx/terminal.tmpl", map[string]interface{}{
"content": loginTemplate,
"renderNavBar": true,
"page": "fax",
})
+1 -1
View File
@@ -15,7 +15,7 @@ import (
// return file
// }
func htmlTemplate(file string, data map[string]interface{}) template.HTML {
func HtmlTemplate(file string, data map[string]interface{}) template.HTML {
templateName := path.Base(file)
tmpl, err := template.New(templateName).ParseFiles(file)
if err != nil {