mirror of
https://github.com/airlabspl/uptimemonitor.git
synced 2025-08-14 20:29:16 +02:00
initial commit
This commit is contained in:
34
handler/logout_handler.go
Normal file
34
handler/logout_handler.go
Normal file
@@ -0,0 +1,34 @@
|
||||
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)
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user