diff --git a/middleware/auth.go b/middleware/auth.go index c377a0f..fbfd491 100644 --- a/middleware/auth.go +++ b/middleware/auth.go @@ -6,6 +6,7 @@ import ( "github.com/gin-gonic/gin" ) +// Passes UserID with `c.Set("UserID")` as it gets id from token func AuthMiddleware() gin.HandlerFunc { return func(c *gin.Context) { authHeader := c.GetHeader("Authorization") @@ -22,6 +23,14 @@ func AuthMiddleware() gin.HandlerFunc { return } + if userID, err := tokens.GetID(token); err != nil { + c.JSON(401, types.ErrorResponse{Message: "Token is invalid ERR4001"}) + c.Abort() + return + } else { + c.Set("UserID", userID) + } + c.Next() } }