Created
May 18, 2023 21:12
-
-
Save bensonmacharia/88add62fdc01477d8d59e71c4725a385 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package util | |
import ( | |
"net/http" | |
"github.com/gin-gonic/gin" | |
) | |
// check for valid admin token | |
func JWTAuth() gin.HandlerFunc { | |
return func(context *gin.Context) { | |
err := ValidateJWT(context) | |
if err != nil { | |
context.JSON(http.StatusUnauthorized, gin.H{"error": "Authentication required"}) | |
context.Abort() | |
return | |
} | |
error := ValidateAdminRoleJWT(context) | |
if error != nil { | |
context.JSON(http.StatusUnauthorized, gin.H{"error": "Only Administrator is allowed to perform this action"}) | |
context.Abort() | |
return | |
} | |
context.Next() | |
} | |
} | |
// check for valid customer token | |
func JWTAuthCustomer() gin.HandlerFunc { | |
return func(context *gin.Context) { | |
err := ValidateJWT(context) | |
if err != nil { | |
context.JSON(http.StatusUnauthorized, gin.H{"error": "Authentication required"}) | |
context.Abort() | |
return | |
} | |
error := ValidateCustomerRoleJWT(context) | |
if error != nil { | |
context.JSON(http.StatusUnauthorized, gin.H{"error": "Only registered Customers are allowed to perform this action"}) | |
context.Abort() | |
return | |
} | |
context.Next() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment