Skip to content

Commit

Permalink
Add user check
Browse files Browse the repository at this point in the history
  • Loading branch information
b-tarczynski committed Oct 27, 2020
1 parent a6c76aa commit 51f54f6
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
3 changes: 3 additions & 0 deletions api/api.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package api

import (
"github.com/BarTar213/notificator/middleware"
"github.com/BarTar213/notificator/models"
"log"
"net/http"
Expand Down Expand Up @@ -95,6 +96,8 @@ func NewApi(options ...func(api *Api)) *Api {

notifications := a.Router.Group("/notifications")
{
notifications.Use(middleware.CheckAccount())

notifications.GET("/:id", nh.GetNotification)
notifications.GET("", nh.ListNotifications)
notifications.PATCH("/:id", nh.UpdateNotification)
Expand Down
7 changes: 7 additions & 0 deletions api/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -54,3 +54,10 @@ func (h *TemplateHandlers) returnTemplate(t *models.Template) {
t.Reset()
h.templatePool.Put(t)
}

//returns account information for user
//should be only used in handler functions that using middleware CheckAccount function
func GetAccount(c *gin.Context) *models.Account {
account := c.Keys["account"].(models.Account)
return &account
}
20 changes: 20 additions & 0 deletions middleware/account.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package middleware

import (
"github.com/BarTar213/notificator/models"
"github.com/gin-gonic/gin"
"net/http"
)

func CheckAccount() gin.HandlerFunc {
return func(c *gin.Context) {
account := models.Account{}
err := c.ShouldBindHeader(&account)
if err != nil {
c.AbortWithStatusJSON(http.StatusForbidden, models.Response{Error: "invalid account headers"})
return
}
c.Set("account", account)
c.Next()
}
}
1 change: 0 additions & 1 deletion utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,3 @@ package utils
const (
EmptyStr = ""
)

0 comments on commit 51f54f6

Please sign in to comment.