CheckAuthenticatedUser retrieves the authenticated user from the context. If the user is not found, it returns an error response.
( ctx context.Context, operation string, newErrorResponse func(code int, message string) T)
| 34 | // If the user is not found, it returns an error response. |
| 35 | func CheckAuthenticatedUser[T any]( |
| 36 | ctx context.Context, |
| 37 | operation string, |
| 38 | newErrorResponse func(code int, message string) T) UserCheckResult[T] { |
| 39 | // At this point, the user should be authenticated and in the context. |
| 40 | // If for some reason the user is not in the context, it is a library or |
| 41 | // internal issue and not an user issue. Return 500 error in that case. |
| 42 | user, found := httpmiddlewares.AuthenticatedUserFromContext(ctx) |
| 43 | if !found { |
| 44 | slog.ErrorContext(ctx, "user not found in context. middleware failure", "operation", operation) |
| 45 | |
| 46 | return UserCheckResult[T]{ |
| 47 | User: nil, |
| 48 | Response: newErrorResponse( |
| 49 | http.StatusInternalServerError, |
| 50 | errMsgInternalServerError, |
| 51 | ), |
| 52 | } |
| 53 | } |
| 54 | |
| 55 | var zeroT T |
| 56 | |
| 57 | return UserCheckResult[T]{ |
| 58 | User: user, |
| 59 | Response: zeroT, |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | // applyPreRequestValidationMiddlewares applies a list of middleware functions to a given http.Handler. |
| 64 | // The middlewares are applied in reverse order to ensure they execute in the order they are defined. |
| 65 | func applyPreRequestValidationMiddlewares(mux *http.ServeMux, |
no outgoing calls
no test coverage detected