Skip to content

Commit 5ab56f0

Browse files
fix: invalid club id error for post creation
1 parent 79ff061 commit 5ab56f0

File tree

1 file changed

+36
-9
lines changed

1 file changed

+36
-9
lines changed

internal/middleware/auth.go

Lines changed: 36 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
package middleware
22

33
import (
4+
"bytes"
5+
"encoding/json"
6+
"io"
47
"net/http"
58
"strconv"
69
"strings"
@@ -133,24 +136,48 @@ func RequireClubMembership(clubRepo repository.ClubRepository) gin.HandlerFunc {
133136
}
134137
}
135138
}
136-
139+
137140
userID, exists := c.Get("user_id")
138141
if !exists {
139142
c.JSON(http.StatusUnauthorized, gin.H{"error": "user not authenticated"})
140143
c.Abort()
141144
return
142145
}
143146

147+
var clubID uint
148+
var err error
149+
144150
clubIDParam := c.Param("id")
151+
145152
if clubIDParam == "" {
146-
clubIDParam = c.Param("club_id")
147-
}
153+
clubID64, parseErr := strconv.ParseUint(clubIDParam, 10, 32)
154+
if parseErr != nil {
155+
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid club ID"})
156+
c.Abort()
157+
return
158+
}
159+
clubID = uint(clubID64)
160+
} else {
161+
bodyBytes, readErr := c.GetRawData()
162+
if readErr != nil {
163+
c.JSON(http.StatusBadRequest, gin.H{"error": "unable to read request body"})
164+
c.Abort()
165+
return
166+
}
148167

149-
clubID, err := strconv.ParseUint(clubIDParam, 10, 32)
150-
if err != nil {
151-
c.JSON(http.StatusBadRequest, gin.H{"error": "invalid club ID"})
152-
c.Abort()
153-
return
168+
c.Request.Body = io.NopCloser(bytes.NewBuffer(bodyBytes))
169+
170+
var reqBody struct {
171+
ClubID uint `json:"club_id"`
172+
}
173+
174+
if json.Unmarshal(bodyBytes, &reqBody) != nil && reqBody.ClubID > 0 {
175+
clubID = reqBody.ClubID
176+
} else {
177+
c.JSON(http.StatusBadRequest, gin.H{"error": "club_id is required in body"})
178+
c.Abort()
179+
return
180+
}
154181
}
155182

156183
membership, err := clubRepo.GetClubMemberByUserID(uint(clubID), userID.(uint))
@@ -235,4 +262,4 @@ func RequireClubMembershipWithRoles(clubRepo repository.ClubRepository, allowedR
235262
c.JSON(http.StatusForbidden, gin.H{"error": "insufficient club membership role"})
236263
c.Abort()
237264
}
238-
}
265+
}

0 commit comments

Comments
 (0)