fix: properly implement JWT user object in middleware
- Update JWT middleware to set complete user object in context - Parse UUID correctly and convert to pgtype.UUID format - Add missing imports for uuid and pgx/v5/pgtype - Fix type conversion from UUID string to byte array - Ensure compatibility with database.Users struct Resolves authentication issues for library and user endpoints
This commit is contained in:
@@ -11,6 +11,8 @@ import (
|
||||
|
||||
"github.com/go-playground/validator/v10"
|
||||
"github.com/golang-jwt/jwt/v5"
|
||||
"github.com/google/uuid"
|
||||
"github.com/jackc/pgx/v5/pgtype"
|
||||
"github.com/labstack/echo-jwt/v4"
|
||||
"github.com/labstack/echo/v4"
|
||||
"github.com/labstack/echo/v4/middleware"
|
||||
@@ -64,6 +66,20 @@ func main() {
|
||||
c.Set("user_role", claims["user_role"])
|
||||
c.Set("user_email", claims["user_email"])
|
||||
c.Set("user_username", claims["user_username"])
|
||||
// Parse UUID from string claims
|
||||
userIDStr, _ := claims["user_id"].(string)
|
||||
userUUID, err := uuid.Parse(userIDStr)
|
||||
if err != nil {
|
||||
c.JSON(http.StatusBadRequest, map[string]string{"error": "invalid user ID in token"})
|
||||
return
|
||||
}
|
||||
|
||||
c.Set("user", database.Users{
|
||||
ID: pgtype.UUID{Bytes: [16]byte(userUUID), Valid: true},
|
||||
Email: claims["user_email"].(string),
|
||||
Username: claims["user_username"].(string),
|
||||
Role: claims["user_role"].(string),
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
|
||||
Reference in New Issue
Block a user