Skip to content

Commit

Permalink
Refactor error handling in AuthHandler middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
funnyzak committed Feb 6, 2024
1 parent 82d20f4 commit 0834700
Showing 1 changed file with 3 additions and 16 deletions.
19 changes: 3 additions & 16 deletions cmd/webserver/middleware/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,21 @@ func AuthHanlder() gin.HandlerFunc {

if err != nil {
log.ZLog.Log.Error().Msgf("Error getting token: %v", err)
c.JSON(http.StatusUnauthorized, &model.ErrorResponse{
Code: http.StatusUnauthorized,
Message: "Unauthorized",
})
c.Abort()
APIUtils.ResponseError(c, http.StatusUnauthorized, "Unauthorized")
return
}

claims := &model.Claims{}
token, err := jwt.ParseWithClaims(tokenString, claims, func(token *jwt.Token) (interface{}, error) {
return []byte(config.Instance.JWT.Secret), nil
})
if err != nil {
log.ZLog.Log.Error().Msgf("Error parsing token: %v", err)
c.JSON(http.StatusUnauthorized, &model.ErrorResponse{
Code: http.StatusUnauthorized,
Message: "Unauthorized",
})
c.Abort()
APIUtils.ResponseError(c, http.StatusUnauthorized, "Unauthorized")
return
}
if !token.Valid {
log.ZLog.Log.Error().Msgf("Invalid token")
c.JSON(http.StatusUnauthorized, &model.ErrorResponse{
Code: http.StatusUnauthorized,
Message: "Invalid token",
})
c.Abort()
APIUtils.ResponseError(c, http.StatusUnauthorized, "Invalid token")
return
}

Expand Down

0 comments on commit 0834700

Please sign in to comment.