Skip to content

Commit

Permalink
fix(http): proper error messages in middlewares
Browse files Browse the repository at this point in the history
  • Loading branch information
mgjules committed Apr 11, 2022
1 parent 3be8cec commit c7f6b32
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions http/middleware.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,18 @@ import (
func (s *Server) unauthenticatedOnly() gin.HandlerFunc {
return func(c *gin.Context) {
if s.spoty.IsAuth() {
c.AbortWithStatusJSON(http.StatusForbidden, gin.H{"error": "you are already authenticated"})
rErr := NewError(
"already-authenticated",
"You are already authenticated.",
http.StatusForbidden,
"You cannot authenticate again as you are already authenticated.",
c.Request.URL.String(),
nil,
)

ctx := c.Request.Context()
s.logger.ErrorwContext(ctx, "failed to authenticate", "error", rErr.Error())
c.AbortWithStatusJSON(http.StatusForbidden, rErr)

return
}
Expand All @@ -21,11 +32,19 @@ func (s *Server) unauthenticatedOnly() gin.HandlerFunc {
func (s *Server) authenticatedOnly() gin.HandlerFunc {
return func(c *gin.Context) {
if !s.spoty.IsAuth() {
c.AbortWithStatusJSON(
rErr := NewError(
"not-authenticated",
"You do not have access.",
http.StatusUnauthorized,
gin.H{"error": "you must be authenticated to access this endpoint"},
"You cannot access this endpoint because you are not authenticated.",
c.Request.URL.String(),
nil,
)

ctx := c.Request.Context()
s.logger.ErrorwContext(ctx, "failed to access endpoint", "error", rErr.Error())
c.AbortWithStatusJSON(http.StatusUnauthorized, rErr)

return
}

Expand Down

0 comments on commit c7f6b32

Please sign in to comment.