Added most basic auth

This commit is contained in:
Pablu23
2024-05-21 16:36:40 +02:00
parent 0904a1214e
commit 63ffb8df6e
10 changed files with 133 additions and 31 deletions

View File

@@ -0,0 +1,24 @@
package server
import (
"net/http"
)
func (s *Server) Auth(next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cookie, err := r.Cookie("auth")
if err != nil {
if r.URL.Path == "/login" || r.URL.Path == "/login/" {
next.ServeHTTP(w, r)
return
}
http.Redirect(w, r, "/login", http.StatusFound)
return
}
if cookie.Value == s.secret {
next.ServeHTTP(w, r)
} else {
http.Redirect(w, r, "/login", http.StatusFound)
}
})
}