Files
mangaGetter/internal/server/middleware.go
2024-05-22 23:00:50 +02:00

21 lines
460 B
Go

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, _ := r.Cookie("auth")
if r.URL.Path == "/login" || r.URL.Path == "/login/" {
next.ServeHTTP(w, r)
return
}
if s.secret == "" || (cookie != nil && cookie.Value == s.secret) {
next.ServeHTTP(w, r)
} else {
http.Redirect(w, r, "/login", http.StatusFound)
}
})
}