Skip to content
This repository has been archived by the owner on Nov 27, 2023. It is now read-only.

Commit

Permalink
New Middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
YamiOdymel committed Feb 8, 2022
1 parent debd0bc commit 90b5b81
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions ginrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,18 +91,20 @@ func Parse(t string, dest interface{}) error {
}

// Middleware gets the JWT from the Authorization header and put it into the *gin.Context for the model to validate.
func Middleware(c *gin.Context, typ interface{}) {
str := strings.Split(c.Request.Header.Get("Authorization"), " ")
if len(str) != 2 {
func Middleware(typ interface{}) func(*gin.Context) {
return func(c *gin.Context) {
str := strings.Split(c.Request.Header.Get("Authorization"), " ")
if len(str) != 2 {
c.Next()
return
}
if err := Parse(str[1], &typ); err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
c.Set(KeyToken, typ)
c.Next()
return
}
if err := Parse(str[1], &typ); err != nil {
c.AbortWithError(http.StatusBadRequest, err)
return
}
c.Set(KeyToken, typ)
c.Next()
}

// Get gets the Token from the *gin.Context, type cast is required.
Expand Down

0 comments on commit 90b5b81

Please sign in to comment.