package middleware import ( "context" "net/http" "watchman/schema" "github.com/golang-jwt/jwt/v5" ) func AdminJwtMiddleware(config schema.ConfigType, next http.Handler) http.Handler { return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { cookie, err := r.Cookie("token") if err != nil { if err == http.ErrNoCookie { w.WriteHeader(http.StatusUnauthorized) return } w.WriteHeader(http.StatusBadRequest) return } tokenStr := cookie.Value claims := &schema.Claims{} token, err := jwt.ParseWithClaims(tokenStr, claims, func(token *jwt.Token) (interface{}, error) { return config.JwtKey, nil }) if err != nil { if err == jwt.ErrSignatureInvalid { w.WriteHeader(http.StatusUnauthorized) return } w.WriteHeader(http.StatusBadRequest) return } if !token.Valid { w.WriteHeader(http.StatusUnauthorized) return } ctx := context.WithValue(r.Context(), schema.UsernameKey{}, claims) next.ServeHTTP(w, r.WithContext(ctx)) }) }