package main import ( "database/sql" "fmt" "net/http" "git.rpuzonas.com/rpuzonas/go-rss-aggregator/internal/auth" ) type authedHandler func(http.ResponseWriter, *http.Request, User) func (cfg *apiConfig) middlewareAuth(handler authedHandler) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { apiKey, err := auth.GetAPIKey(r.Header) if err != nil { respondWithError(w, 403, fmt.Sprintf("Auth error: %v", err)) return } user, err := cfg.DB.GetUserByAPIKey(r.Context(), sql.NullString{String: apiKey, Valid: true}) if err != nil { respondWithError(w, 400, fmt.Sprintf("Couldn't get user: %v", err)) return } handler(w, r, databaseUserToUser(user)) } }