Skip to content

Commit fc491f4

Browse files
committed
refactor getUserId util
1 parent 21f2443 commit fc491f4

File tree

3 files changed

+9
-9
lines changed

3 files changed

+9
-9
lines changed

packages/backend/pkg/handler/items.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import (
1313
func (h *Handler) getAllItems(c *gin.Context) {
1414
userId, err := getUserId(c)
1515
if err != nil {
16-
responseWithError(c, models.NewRequestError(http.StatusBadRequest, err))
16+
responseWithError(c, err)
1717
return
1818
}
1919

@@ -44,7 +44,7 @@ func (h *Handler) getAllItems(c *gin.Context) {
4444
func (h *Handler) createItem(c *gin.Context) {
4545
userId, err := getUserId(c)
4646
if err != nil {
47-
responseWithError(c, models.NewRequestError(http.StatusUnauthorized, err))
47+
responseWithError(c, err)
4848
return
4949
}
5050

packages/backend/pkg/handler/lists.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
func (h *Handler) getAllLists(c *gin.Context) {
1313
userId, err := getUserId(c)
1414
if err != nil {
15-
responseWithError(c, models.NewRequestError(http.StatusUnauthorized, err))
15+
responseWithError(c, err)
1616
return
1717
}
1818

@@ -39,7 +39,7 @@ func (input todoListInput) Validate() error {
3939
func (h *Handler) createList(c *gin.Context) {
4040
userId, err := getUserId(c)
4141
if err != nil {
42-
responseWithError(c, models.NewRequestError(http.StatusUnauthorized, err))
42+
responseWithError(c, err)
4343
return
4444
}
4545

@@ -66,7 +66,7 @@ func (h *Handler) createList(c *gin.Context) {
6666
func (h *Handler) getListById(c *gin.Context) {
6767
userId, err := getUserId(c)
6868
if err != nil {
69-
responseWithError(c, models.NewRequestError(http.StatusUnauthorized, err))
69+
responseWithError(c, err)
7070
return
7171
}
7272

@@ -88,7 +88,7 @@ func (h *Handler) getListById(c *gin.Context) {
8888
func (h *Handler) updateList(c *gin.Context) {
8989
userId, err := getUserId(c)
9090
if err != nil {
91-
responseWithError(c, models.NewRequestError(http.StatusUnauthorized, err))
91+
responseWithError(c, err)
9292
return
9393
}
9494

@@ -121,7 +121,7 @@ func (h *Handler) updateList(c *gin.Context) {
121121
func (h *Handler) deleteList(c *gin.Context) {
122122
userId, err := getUserId(c)
123123
if err != nil {
124-
responseWithError(c, models.NewRequestError(http.StatusUnauthorized, err))
124+
responseWithError(c, err)
125125
return
126126
}
127127

packages/backend/pkg/handler/middleware.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ func (h *Handler) authRequired(c *gin.Context) {
3333
func getUserId(c *gin.Context) (int, error) {
3434
userId, ok := c.Get(userCtx)
3535
if !ok {
36-
return 0, errors.New("userId is invalid")
36+
return 0, models.NewRequestError(http.StatusUnauthorized, errors.New("userId is invalid"))
3737
}
3838

3939
userIdInt, ok := userId.(int)
4040
if !ok {
41-
return 0, errors.New("userId is invalid")
41+
return 0, models.NewRequestError(http.StatusUnauthorized, errors.New("userId is invalid"))
4242
}
4343

4444
return userIdInt, nil

0 commit comments

Comments
 (0)