Skip to content

Commit

Permalink
hx-redirect
Browse files Browse the repository at this point in the history
  • Loading branch information
mstgnz committed Jun 17, 2024
1 parent b54c6db commit 02ca19e
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 33 deletions.
17 changes: 6 additions & 11 deletions handler/web/user.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package web

import (
"encoding/json"
"net/http"
"strconv"
"time"
Expand All @@ -24,19 +23,17 @@ func (h *UserHandler) LoginHandler(w http.ResponseWriter, r *http.Request) error
if response.Status && code == http.StatusOK {
user, ok := response.Data["user"].(*models.User)
if ok && user.ID > 0 {
w.Header().Set("HX-Redirect", "/")
http.SetCookie(w, &http.Cookie{
Name: "auth",
Value: strconv.Itoa(user.ID),
Expires: time.Now().Add(12 * time.Hour),
})
}
}
json.NewEncoder(w).Encode(response)
return nil
default:
json.NewEncoder(w).Encode(map[string]any{"status": false, "message": "not supported request", "data": nil})
return nil
w.Write([]byte(response.Message))
}
return nil
}

func (h *UserHandler) RegisterHandler(w http.ResponseWriter, r *http.Request) error {
Expand All @@ -48,19 +45,17 @@ func (h *UserHandler) RegisterHandler(w http.ResponseWriter, r *http.Request) er
if response.Status && code == http.StatusCreated {
user, ok := response.Data["user"].(*models.User)
if ok && user.ID > 0 {
w.Header().Set("HX-Redirect", "/")
http.SetCookie(w, &http.Cookie{
Name: "auth",
Value: strconv.Itoa(user.ID),
Expires: time.Now().Add(12 * time.Hour),
})
}
}
json.NewEncoder(w).Encode(response)
return nil
default:
json.NewEncoder(w).Encode(map[string]any{"status": false, "message": "not supported request", "data": nil})
return nil
w.Write([]byte(response.Message))
}
return nil
}

func (h *UserHandler) HomeHandler(w http.ResponseWriter, _ *http.Request) error {
Expand Down
11 changes: 1 addition & 10 deletions views/pages/login.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
{{define "content"}}
<div class="bg-white p-8 rounded-lg shadow-md w-96">
<h2 class="text-xl mb-4">Sign In</h2>
<form hx-post="/login" hx-target="#toast" hx-ext="json-enc" hx-on="htmx:afterRequest: handleLoginResponse(event)" novalidate autocomplete="off">
<form hx-post="/login" hx-target="#toast" hx-ext="json-enc" novalidate autocomplete="off">
<div class="mb-4">
<label class="block text-gray-700 text-sm font-bold mb-2" for="email">
Email
Expand All @@ -25,13 +25,4 @@
<span id="toast"></span>
</form>
</div>
<script>
function handleLoginResponse(event) {
const response = JSON.parse(event.detail.xhr.response);
document.querySelector('#toast').innerHTML = response.message;
if (response.status) {
window.location.href = "/";
}
}
</script>
{{end}}
12 changes: 0 additions & 12 deletions views/pages/register.gohtml
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,4 @@
<span id="toast"></span>
</form>
</div>
{{end}}

{{define "js"}}
<script>
document.body.addEventListener('htmx:afterRequest', function(evt) {
const response = JSON.parse(evt.detail.xhr.response)
document.querySelector('#toast').innerHTML = response.message;
if (response.status) {
window.location.href = "/";
}
});
</script>
{{end}}

0 comments on commit 02ca19e

Please sign in to comment.