From c959d9f173daba311ebba12905ea145a47d95e4b Mon Sep 17 00:00:00 2001 From: qowevisa Date: Sat, 3 Aug 2024 08:43:08 +0300 Subject: [PATCH] Add middleware package with auth.go --- middleware/auth.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 middleware/auth.go diff --git a/middleware/auth.go b/middleware/auth.go new file mode 100644 index 0000000..c377a0f --- /dev/null +++ b/middleware/auth.go @@ -0,0 +1,27 @@ +package middleware + +import ( + "git.qowevisa.me/Qowevisa/gonuts/tokens" + "git.qowevisa.me/Qowevisa/gonuts/types" + "github.com/gin-gonic/gin" +) + +func AuthMiddleware() gin.HandlerFunc { + return func(c *gin.Context) { + authHeader := c.GetHeader("Authorization") + if authHeader == "" { + c.JSON(401, types.ErrorResponse{Message: "Authorization header is required"}) + c.Abort() + return + } + + token := authHeader + if !tokens.AmIAllowed(token) { + c.JSON(401, types.ErrorResponse{Message: "Token is invalid"}) + c.Abort() + return + } + + c.Next() + } +}