From 63c96edc88e6628c6f4cd02fd8808840de97ef63 Mon Sep 17 00:00:00 2001 From: qowevisa Date: Sat, 3 Aug 2024 08:50:06 +0300 Subject: [PATCH] Add setting userID from token as in middelware --- middleware/auth.go | 9 +++++++++ 1 file changed, 9 insertions(+) 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() } }