From 71fe05f9165798e13fabc30d04e8c7e499615a5a Mon Sep 17 00:00:00 2001 From: Eduardo Silva Date: Mon, 10 Jun 2024 22:38:16 +0100 Subject: [PATCH] Better flow when trying to send a message but the token is out of date --- src/controllers/authentication.go | 7 ------- src/controllers/sendFax.go | 6 +++--- 2 files changed, 3 insertions(+), 10 deletions(-) diff --git a/src/controllers/authentication.go b/src/controllers/authentication.go index 67362f8..77fa8a9 100644 --- a/src/controllers/authentication.go +++ b/src/controllers/authentication.go @@ -100,7 +100,6 @@ func createNewSession(w http.ResponseWriter, loginForm models.LoginForm) { log.Printf(`| {"user": %s, "time": %d}`, loginForm.Username, time.Now().Unix()) w.Header().Add("HX-Redirect", "/editor") - } func updateSession(w http.ResponseWriter, loginForm models.LoginForm, @@ -108,7 +107,6 @@ func updateSession(w http.ResponseWriter, loginForm models.LoginForm, localUserSession := models.GetSession(sessionCookie.Value) if localUserSession == nil { // Just create a new session in this case - log.Println("| User has session key not present locally") createNewSession(w, loginForm) return } @@ -121,7 +119,6 @@ func updateSession(w http.ResponseWriter, loginForm models.LoginForm, } http.SetCookie(w, &cookie) - log.Println("| User is being sent to ", localUserSession.Wants) w.Header().Set("HX-Redirect", localUserSession.Wants) } @@ -137,7 +134,6 @@ func Authenticate(w http.ResponseWriter, r *http.Request) { } if !models.ValidateCredentials(&loginForm) { - log.Println("| Invalid credentials.") w.Header().Add("HX-Trigger", `{"wrongCredentials":"Wrong Credentials"}`) return } @@ -175,13 +171,10 @@ func saveSessionState(r *http.Request) { } cache.Data[tok].Wants = r.RequestURI - - log.Println("| User wants to go to ", cache.Data[tok].Wants) } } func reauth(w http.ResponseWriter, r *http.Request) { - log.Println("| User wants to request something but authentication has expired") saveSessionState(r) w.Header().Set("HX-Redirect", "/login") w.Header().Set("HX-Push-Url", "true") diff --git a/src/controllers/sendFax.go b/src/controllers/sendFax.go index d4441c1..610deda 100644 --- a/src/controllers/sendFax.go +++ b/src/controllers/sendFax.go @@ -70,10 +70,10 @@ func sendRequestToPrinter(msg string) (PrintServerResponse, error) { func SendFax(w http.ResponseWriter, r *http.Request) { authd, token := isAuthenticated(r) if !authd { - // We don't do a reauth here so that the user is actually sent to the - // login page and then to the /editor by default + // We don't do a reauth here so we do a simple refresh and let the /editor controller + // take care of it on the following request // - We will have to make it be clear that the message was not sent tho - w.Header().Set("HX-Redirect", "/login") + w.Header().Set("HX-Refresh", "true") return }