refactor: migrate from templ to html/template
This commit is contained in:
@@ -4,7 +4,7 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
|
||||
"mal/web/templates"
|
||||
"mal/templates"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
@@ -21,16 +21,26 @@ func rateLimitErrorFromQuery(r *http.Request) string {
|
||||
if r.URL.Query().Get("error") == "rate_limited" {
|
||||
return rateLimitFormError
|
||||
}
|
||||
|
||||
return ""
|
||||
}
|
||||
|
||||
func (h *Handler) HandleLoginPage(w http.ResponseWriter, r *http.Request) {
|
||||
err := templates.GetRenderer().ExecuteTemplate(w, "login.gohtml", map[string]any{
|
||||
"Error": rateLimitErrorFromQuery(r),
|
||||
"Username": "",
|
||||
})
|
||||
if err != nil {
|
||||
log.Printf("render error: %v", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
}
|
||||
}
|
||||
|
||||
func (h *Handler) HandleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
if err := r.ParseForm(); err != nil {
|
||||
if renderErr := templates.Login("Something went wrong. Please try again.", "").Render(r.Context(), w); renderErr != nil {
|
||||
log.Printf("render error: %v", renderErr)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
}
|
||||
templates.GetRenderer().ExecuteTemplate(w, "login.gohtml", map[string]any{
|
||||
"Error": "Something went wrong. Please try again.",
|
||||
"Username": "",
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
@@ -38,34 +48,23 @@ func (h *Handler) HandleLogin(w http.ResponseWriter, r *http.Request) {
|
||||
password := r.FormValue("password")
|
||||
|
||||
if username == "" || password == "" {
|
||||
if renderErr := templates.Login("The email or password is wrong.", username).Render(r.Context(), w); renderErr != nil {
|
||||
log.Printf("render error: %v", renderErr)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
}
|
||||
templates.GetRenderer().ExecuteTemplate(w, "login.gohtml", map[string]any{
|
||||
"Error": "The email or password is wrong.",
|
||||
"Username": username,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
session, err := h.authService.Login(r.Context(), username, password)
|
||||
if err != nil {
|
||||
if renderErr := templates.Login("The email or password is wrong.", username).Render(r.Context(), w); renderErr != nil {
|
||||
log.Printf("render error: %v", renderErr)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
}
|
||||
templates.GetRenderer().ExecuteTemplate(w, "login.gohtml", map[string]any{
|
||||
"Error": "The email or password is wrong.",
|
||||
"Username": username,
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
SetSessionCookie(w, session.ID, session.ExpiresAt)
|
||||
|
||||
if r.Header.Get("HX-Request") == "true" {
|
||||
w.Header().Set("HX-Redirect", "/")
|
||||
return
|
||||
}
|
||||
http.Redirect(w, r, "/", http.StatusFound)
|
||||
}
|
||||
|
||||
func (h *Handler) HandleLoginPage(w http.ResponseWriter, r *http.Request) {
|
||||
if err := templates.Login(rateLimitErrorFromQuery(r), "").Render(r.Context(), w); err != nil {
|
||||
log.Printf("render error: %v", err)
|
||||
http.Error(w, "Internal Server Error", http.StatusInternalServerError)
|
||||
}
|
||||
http.Redirect(w, r, "/", http.StatusSeeOther)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user