Skip to content

Commit

Permalink
DEVPROD-12802 Ensure userId AttributeStore reads and writes are not s…
Browse files Browse the repository at this point in the history
…tale (#500)
  • Loading branch information
khelif96 authored Nov 20, 2024
1 parent a1d8c5c commit 9b2eb40
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 7 deletions.
4 changes: 1 addition & 3 deletions apps/parsley/src/analytics/useAnalyticAttributes.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { useEffect } from "react";
import { useLogContext } from "context/LogContext";

export const useAnalyticAttributes = () => {
export const useAnalyticAttributes = (userId: string) => {
const { logMetadata } = useLogContext();
const { logType, renderingType } = logMetadata || {};
const { AttributeStore } = window;

const userId = localStorage.getItem("userId");

useEffect(() => {
if (!AttributeStore) {
console.error("AttributeStore not found in window object");
Expand Down
2 changes: 1 addition & 1 deletion apps/parsley/src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ const Content: React.FC = () => {
const { user } = useUser();
localStorage.setItem("userId", user?.userId ?? "");

useAnalyticAttributes();
useAnalyticAttributes(user?.userId ?? "");
const { isAuthenticated } = useAuthContext();
return isAuthenticated ? (
<Routes>
Expand Down
3 changes: 1 addition & 2 deletions apps/spruce/src/analytics/useAnalyticsAttributes.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { useEffect } from "react";

export const useAnalyticsAttributes = () => {
const userId = localStorage.getItem("userId");
export const useAnalyticsAttributes = (userId: string) => {
const { AttributeStore } = window;

useEffect(() => {
Expand Down
2 changes: 1 addition & 1 deletion apps/spruce/src/components/Content/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@ const shouldDisableForTest =

export const Layout: React.FC = () => {
const { isAuthenticated } = useAuthStateContext();
useAnalyticsAttributes();
useAnnouncementToast();

// this top-level query is required for authentication to work
// afterware is used at apollo link level to authenticate or deauthenticate user based on response to query
// therefore this could be any query as long as it is top-level
const { data } = useQuery<UserQuery, UserQueryVariables>(USER);
useAnalyticsAttributes(data?.user?.userId ?? "");
localStorage.setItem("userId", data?.user?.userId ?? "");
const { userSettings } = useUserSettings();
const { useSpruceOptions } = userSettings ?? {};
Expand Down

0 comments on commit 9b2eb40

Please sign in to comment.