Create admin only middleware

This commit is contained in:
Dominic Ferrando
2025-04-23 14:23:25 -04:00
parent cdef2c03be
commit a0f268b0d3
2 changed files with 13 additions and 1 deletions
+12
View File
@@ -3,6 +3,7 @@ package util
import (
"crypto/rand"
"encoding/hex"
"net/http"
"sync"
"time"
)
@@ -65,3 +66,14 @@ func HasExistingAdminSession(username string) bool {
return false
}
func AdminOnlyMiddleware(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c, err := r.Cookie("comfy_admin")
if err != nil || !IsAdminSessionValid(c.Value) {
http.Error(w, "Unauthorized", http.StatusUnauthorized)
return
}
next.ServeHTTP(w, r)
})
}