M sdbapi/apierror/apierror.go => sdbapi/apierror/apierror.go +2 -0
@@ 13,6 13,8 @@ var (
Placeholder APIError = APIError{1000, "Placeholder", "placeholder"}
InvalidInput APIError = APIError{1001, "InvalidInput", "invalid input"}
NotAuthorized APIError = APIError{1002, "NotAuthorized", "not authorized"}
+ MissingAuth APIError = APIError{1003, "MissingAuthorization", "missing authorization"}
+ GenericJWTError APIError = APIError{1004, "GenericJWTError", ""}
)
// user related errors
M sdbapi/middlewares/auth.go => sdbapi/middlewares/auth.go +4 -2
@@ 10,6 10,7 @@ package middlewares
import (
"api/auth"
+ "api/apierror"
"net/http"
"github.com/gin-gonic/gin"
@@ 22,7 23,7 @@ func Auth() gin.HandlerFunc {
token := c.Request.Header.Get("Authorization")
if token == "" {
// no authorization header
- c.JSON(http.StatusUnauthorized, gin.H{"error": "missing authorization"})
+ c.JSON(http.StatusUnauthorized, gin.H{"error": apierror.MissingAuth})
c.Abort()
return
}
@@ 31,7 32,8 @@ func Auth() gin.HandlerFunc {
claims, err := auth.ValidateJWTToken(token)
if err != nil {
// something is wrong with the token, error out
- c.JSON(http.StatusUnauthorized, gin.H{"error": err.Error()})
+ jwterror := apierror.APIError{apierror.GenericJWTError.ID, apierror.GenericJWTError.Name, err.Error()}
+ c.JSON(http.StatusUnauthorized, gin.H{"error": jwterror})
c.Abort()
return
}