ai cooking
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

get user id on the logged context (#456)

* get user id on the logged context

* fumpt

---------

Co-authored-by: paul miller <paul.miller>

authored by

Paul Miller
paul miller
and committed by
GitHub
c87e4956 07203d98

+23 -23
+1 -1
internal/admin/middleware.go
··· 44 44 45 45 email, err := m.auth.GetUserEmail(r.Context(), userID) 46 46 if err != nil { 47 - slog.WarnContext(r.Context(), "admin email lookup failed", "user_id", userID, "error", err) 47 + slog.WarnContext(r.Context(), "admin email lookup failed", "error", err) 48 48 http.NotFound(w, r) 49 49 return 50 50 }
-9
internal/auth/clerk.go
··· 97 97 return sessionClaims.Subject, nil 98 98 } 99 99 100 - func (c *clerkClient) FromRequest(ctx context.Context, req *http.Request) (string, error) { 101 - clerkUserID, err := c.GetUserIDFromRequest(req) 102 - if err != nil { 103 - return "", err 104 - } 105 - slog.InfoContext(ctx, "found clerk user ID", "clerk_user_id", clerkUserID) 106 - return clerkUserID, nil 107 - } 108 - 109 100 // WithClerkHTTP wraps the http.Handler with Clerk's authentication middleware 110 101 func (c *clerkClient) WithAuthHTTP(handler http.Handler) http.Handler { 111 102 purgeAndRedirect := clerkhttp.AuthorizationFailureHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+8
internal/logsetup/context.go
··· 3 3 import ( 4 4 "context" 5 5 "log/slog" 6 + 7 + "github.com/clerk/clerk-sdk-go/v2" 6 8 ) 7 9 8 10 type contextKey string ··· 68 70 if sessionID, ok := SessionIDFromContext(ctx); ok { 69 71 record.AddAttrs(slog.String("session_id", sessionID)) 70 72 } 73 + // hard dependency on clerk is bad. but plumbg an auth 74 + sessionClaims, ok := clerk.SessionClaimsFromContext(ctx) 75 + if ok && sessionClaims != nil { 76 + record.AddAttrs(slog.String("user_id", sessionClaims.Subject)) 77 + } 78 + 71 79 return h.handler.Handle(ctx, record) 72 80 } 73 81
+12 -12
internal/recipes/server.go
··· 582 582 } 583 583 selection, err := s.loadRecipeSelection(ctx, currentUser.ID, selectionHash) 584 584 if err != nil { 585 - slog.ErrorContext(ctx, "failed to load recipe selection for save", "user_id", currentUser.ID, "selection_hash", selectionHash, "error", err) 585 + slog.ErrorContext(ctx, "failed to load recipe selection for save", "selection_hash", selectionHash, "error", err) 586 586 http.Error(w, "failed to save recipe", http.StatusInternalServerError) 587 587 return 588 588 } 589 589 selection.markSaved(recipeHash) 590 590 if err := s.saveRecipeSelection(ctx, currentUser.ID, selectionHash, selection); err != nil { 591 - slog.ErrorContext(ctx, "failed to save recipe selection", "user_id", currentUser.ID, "selection_hash", selectionHash, "error", err) 591 + slog.ErrorContext(ctx, "failed to save recipe selection", "selection_hash", selectionHash, "error", err) 592 592 http.Error(w, "failed to save recipe", http.StatusInternalServerError) 593 593 return 594 594 } ··· 606 606 } 607 607 608 608 if err := s.saveRecipesToUserProfile(ctx, currentUser, *recipe); err != nil { 609 - slog.ErrorContext(ctx, "failed to save recipe to user profile", "user_id", currentUser.ID, "hash", recipeHash, "error", err) 609 + slog.ErrorContext(ctx, "failed to save recipe to user profile", "hash", recipeHash, "error", err) 610 610 http.Error(w, "failed to save recipe", http.StatusInternalServerError) 611 611 return 612 612 } 613 613 614 614 p, err := s.paramsForAction(ctx, selectionHash, currentUser.ID, "") 615 615 if err != nil { 616 - slog.ErrorContext(ctx, "failed to load params for save response", "user_id", currentUser.ID, "selection_hash", selectionHash, "error", err) 616 + slog.ErrorContext(ctx, "failed to load params for save response", "selection_hash", selectionHash, "error", err) 617 617 http.Error(w, "failed to save recipe", http.StatusInternalServerError) 618 618 return 619 619 } ··· 672 672 } 673 673 selection, err := s.loadRecipeSelection(ctx, currentUser.ID, selectionHash) 674 674 if err != nil { 675 - slog.ErrorContext(ctx, "failed to load recipe selection for dismiss", "user_id", currentUser.ID, "selection_hash", selectionHash, "error", err) 675 + slog.ErrorContext(ctx, "failed to load recipe selection for dismiss", "selection_hash", selectionHash, "error", err) 676 676 http.Error(w, "failed to dismiss recipe", http.StatusInternalServerError) 677 677 return 678 678 } 679 679 selection.markDismissed(recipeHash) 680 680 if err := s.saveRecipeSelection(ctx, currentUser.ID, selectionHash, selection); err != nil { 681 - slog.ErrorContext(ctx, "failed to save recipe selection for dismiss", "user_id", currentUser.ID, "selection_hash", selectionHash, "error", err) 681 + slog.ErrorContext(ctx, "failed to save recipe selection for dismiss", "selection_hash", selectionHash, "error", err) 682 682 http.Error(w, "failed to dismiss recipe", http.StatusInternalServerError) 683 683 return 684 684 } 685 685 686 686 if err := s.removeRecipeFromUserProfile(ctx, *currentUser, recipeHash); err != nil { 687 - slog.ErrorContext(ctx, "failed to remove recipe from user profile", "user_id", currentUser.ID, "hash", recipeHash, "error", err) 687 + slog.ErrorContext(ctx, "failed to remove recipe from user profile", "hash", recipeHash, "error", err) 688 688 http.Error(w, "failed to dismiss recipe", http.StatusInternalServerError) 689 689 return 690 690 } 691 691 692 692 p, err := s.paramsForAction(ctx, selectionHash, currentUser.ID, "") 693 693 if err != nil { 694 - slog.ErrorContext(ctx, "failed to load params for dismiss response", "user_id", currentUser.ID, "selection_hash", selectionHash, "error", err) 694 + slog.ErrorContext(ctx, "failed to load params for dismiss response", "selection_hash", selectionHash, "error", err) 695 695 http.Error(w, "failed to dismiss recipe", http.StatusInternalServerError) 696 696 return 697 697 } ··· 934 934 if signedIn { 935 935 fromStore, selErr := s.loadRecipeSelection(ctx, userID, hashParam) 936 936 if selErr != nil { 937 - slog.ErrorContext(ctx, "failed to load recipe selection for render", "user_id", userID, "hash", hashParam, "error", selErr) 937 + slog.ErrorContext(ctx, "failed to load recipe selection for render", "hash", hashParam, "error", selErr) 938 938 http.Error(w, "failed to load recipe selection", http.StatusInternalServerError) 939 939 return 940 940 } ··· 1030 1030 return r.Title, cooked[r.Hash].Cooked 1031 1031 }) 1032 1032 1033 - slog.InfoContext(ctx, "generating cached recipes", "params", p.String(), "hash", hash, "user_id", currentUser.ID) 1033 + slog.InfoContext(ctx, "generating cached recipes", "params", p.String(), "hash", hash) 1034 1034 shoppingList, err := s.generator.GenerateRecipes(ctx, p) 1035 1035 if err != nil { 1036 1036 slog.ErrorContext(ctx, "generate error", "error", err) ··· 1132 1132 if err := s.storage.Update(currentUser); err != nil { 1133 1133 return fmt.Errorf("failed to update user with saved recipes: %w", err) 1134 1134 } 1135 - slog.InfoContext(ctx, "added saved recipe to user profile", "user_id", currentUser.ID, "title", recipe.Title) 1135 + slog.InfoContext(ctx, "added saved recipe to user profile", "title", recipe.Title) 1136 1136 1137 1137 return nil 1138 1138 } ··· 1155 1155 if err := s.storage.Update(&currentUser); err != nil { 1156 1156 return fmt.Errorf("failed to update user when dismissing recipe: %w", err) 1157 1157 } 1158 - slog.InfoContext(ctx, "removed recipe from user profile", "user_id", currentUser.ID, "hash", recipeHash) 1158 + slog.InfoContext(ctx, "removed recipe from user profile", "hash", recipeHash) 1159 1159 return nil 1160 1160 }
+2 -1
internal/users/storage.go
··· 13 13 14 14 "careme/internal/auth" 15 15 "careme/internal/cache" 16 + 16 17 utypes "careme/internal/users/types" 17 18 ) 18 19 ··· 59 60 } 60 61 defer func() { 61 62 if err := userBytes.Close(); err != nil { 62 - slog.Error("failed to close user reader", "error", err, "user_id", id) 63 + slog.Error("failed to close user reader", "error", err) 63 64 } 64 65 }() 65 66 decoder := json.NewDecoder(userBytes)