mirror of
https://github.com/airlabspl/uptimemonitor.git
synced 2025-08-14 12:19:19 +02:00
35 lines
785 B
Go
35 lines
785 B
Go
package handler
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
"uptimemonitor"
|
|
)
|
|
|
|
func (h *Handler) Logout() http.HandlerFunc {
|
|
return func(w http.ResponseWriter, r *http.Request) {
|
|
session, ok := r.Context().Value(sessionContextKey).(uptimemonitor.Session)
|
|
if !ok {
|
|
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
|
http.Error(w, http.StatusText(http.StatusUnauthorized), http.StatusServiceUnavailable)
|
|
return
|
|
}
|
|
|
|
err := h.Store.RemoveSessionByID(r.Context(), session.ID)
|
|
if err != nil {
|
|
// todo: log
|
|
}
|
|
|
|
http.SetCookie(w, &http.Cookie{
|
|
Name: "session",
|
|
Value: "",
|
|
HttpOnly: true,
|
|
SameSite: http.SameSiteLaxMode,
|
|
Secure: h.Secure,
|
|
Expires: time.Now().Add(time.Hour * 24 * 30 * -1),
|
|
})
|
|
|
|
http.Redirect(w, r, "/login", http.StatusSeeOther)
|
|
}
|
|
}
|