Create admin only middleware
This commit is contained in:
@@ -3,6 +3,7 @@ package util
|
|||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"encoding/hex"
|
"encoding/hex"
|
||||||
|
"net/http"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
)
|
)
|
||||||
@@ -65,3 +66,14 @@ func HasExistingAdminSession(username string) bool {
|
|||||||
|
|
||||||
return false
|
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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|||||||
+1
-1
@@ -70,7 +70,7 @@ func main() {
|
|||||||
// -----------------
|
// -----------------
|
||||||
|
|
||||||
// INDEX PAGE
|
// INDEX PAGE
|
||||||
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
|
r.With(util.AdminOnlyMiddleware).Get("/", func(w http.ResponseWriter, r *http.Request) {
|
||||||
views.Index().Render(r.Context(), w)
|
views.Index().Render(r.Context(), w)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user