Skip to content

Instantly share code, notes, and snippets.

@bensonmacharia
Created May 18, 2023 21:12
Show Gist options
  • Save bensonmacharia/88add62fdc01477d8d59e71c4725a385 to your computer and use it in GitHub Desktop.
Save bensonmacharia/88add62fdc01477d8d59e71c4725a385 to your computer and use it in GitHub Desktop.
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