package handlers import ( "bookhoard/internal/database" "net/http" "github.com/labstack/echo/v5" ) // GetAuthenticatedUser safely retrieves the authenticated user from context // Returns an error if the user is not found in context or type assertion fails func GetAuthenticatedUser(c *echo.Context) (database.Users, error) { user, ok := c.Get("user").(database.Users) if !ok { return database.Users{}, echo.NewHTTPError(http.StatusInternalServerError, "authentication context error") } return user, nil } // MustGetAuthenticatedUser gets user or panics // Only use this after authentication middleware has verified the user // Panicking here indicates a serious bug in the middleware chain func MustGetAuthenticatedUser(c *echo.Context) database.Users { user, err := GetAuthenticatedUser(c) if err != nil { panic(err) // Should never happen if authentication middleware is working correctly } return user }