You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
103 lines
2.6 KiB
103 lines
2.6 KiB
package handlers
|
|
|
|
import (
|
|
"html/template"
|
|
"log"
|
|
"marmic/servicetrade-toolbox/internal/api"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
func LoginHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method == "GET" {
|
|
tmpl := template.Must(template.ParseFiles("templates/login.html"))
|
|
tmpl.Execute(w, nil)
|
|
return
|
|
}
|
|
|
|
if r.Method == "POST" {
|
|
email := r.FormValue("email")
|
|
password := r.FormValue("password")
|
|
|
|
session := api.NewSession()
|
|
err := session.Login(email, password)
|
|
if err != nil {
|
|
if r.Header.Get("HX-Request") == "true" {
|
|
w.Write([]byte("<div class='error'>Login failed: " + err.Error() + "</div>"))
|
|
} else {
|
|
http.Error(w, "Login failed", http.StatusUnauthorized)
|
|
}
|
|
return
|
|
}
|
|
cookieParts := strings.Split(session.Cookie, ";")
|
|
sessionId := strings.TrimPrefix(cookieParts[0], "PHPSESSID=")
|
|
// Set session cookie
|
|
http.SetCookie(w, &http.Cookie{
|
|
Name: "PHPSESSID",
|
|
Value: sessionId,
|
|
Path: "/",
|
|
HttpOnly: true,
|
|
Secure: r.TLS != nil,
|
|
SameSite: http.SameSiteLaxMode,
|
|
})
|
|
|
|
if r.Header.Get("HX-Request") == "true" {
|
|
w.Header().Set("HX-Redirect", "/")
|
|
w.WriteHeader(http.StatusOK)
|
|
w.Write([]byte("Login successful"))
|
|
} else {
|
|
http.Redirect(w, r, "/", http.StatusSeeOther)
|
|
}
|
|
}
|
|
}
|
|
|
|
func LogoutHandler(w http.ResponseWriter, r *http.Request) {
|
|
cookie, err := r.Cookie("PHPSESSID")
|
|
if err != nil {
|
|
log.Printf("No session cookie found: %v", err)
|
|
|
|
// Check if the request is an HTMX request
|
|
if r.Header.Get("HX-Request") != "" {
|
|
// Use HX-Redirect to redirect the entire page to the login page
|
|
w.Header().Set("HX-Redirect", "/login")
|
|
w.WriteHeader(http.StatusOK)
|
|
} else {
|
|
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
|
}
|
|
return
|
|
}
|
|
|
|
session := api.NewSession()
|
|
session.Cookie = "PHPSESSID=" + cookie.Value
|
|
|
|
err = session.Logout()
|
|
if err != nil {
|
|
log.Printf("Logout failed: %v", err)
|
|
http.Error(w, "Logout failed", http.StatusInternalServerError)
|
|
return
|
|
}
|
|
|
|
// Clear the session cookie
|
|
http.SetCookie(w, &http.Cookie{
|
|
Name: "PHPSESSID",
|
|
Value: "",
|
|
Path: "/",
|
|
MaxAge: -1,
|
|
HttpOnly: true,
|
|
Secure: r.TLS != nil,
|
|
SameSite: http.SameSiteLaxMode,
|
|
})
|
|
|
|
log.Println("Logout successful, redirecting to login page")
|
|
|
|
// Check if the request is an HTMX request
|
|
if r.Header.Get("HX-Request") != "" {
|
|
// Use HX-Redirect to ensure the entire page is redirected to the login page
|
|
w.Header().Set("HX-Redirect", "/login")
|
|
w.WriteHeader(http.StatusOK)
|
|
} else {
|
|
// If not an HTMX request, perform a full-page redirect
|
|
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
|
}
|
|
}
|
|
|