Skip to content

Commit

Permalink
Merge pull request #417 from MuckRock/fix/getMe-too-many-calls
Browse files Browse the repository at this point in the history
Prevent calls to `getMe` from embed
  • Loading branch information
allanlasser authored Jan 26, 2024
2 parents d301acd + f8a976a commit 84edb5e
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 1 deletion.
1 change: 1 addition & 0 deletions .storybook/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const config: StorybookConfig = {
"@storybook/addon-essentials",
"@storybook/addon-interactions",
"@storybook/addon-svelte-csf",
"storybook-addon-cookie",
],
framework: {
name: "@storybook/svelte-webpack5",
Expand Down
4 changes: 4 additions & 0 deletions .storybook/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ const preview: Preview = {
date: /Date$/,
},
},
cookie: {
csrftoken: "mockToken",
},
cookiePreserve: true,
},
};

Expand Down
24 changes: 24 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@
"react-dom": "^18.2.0",
"serve": "^14.2.0",
"storybook": "7.6.10",
"storybook-addon-cookie": "^3.2.0",
"storybook-mock-date-decorator": "^1.0.1",
"svelte-jester": "^3.0.0",
"tape": "^5.7.2",
Expand Down
5 changes: 4 additions & 1 deletion src/api/orgAndUser.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import session from "./session.js";
import session, { cookiesEnabled, getCsrfToken } from "./session.js";
import { USER_EXPAND, ORG_EXPAND, DEFAULT_EXPAND } from "./common.js";
import { queryBuilder } from "@/util/url.js";
import { grabAllPages } from "@/util/paginate.js";
import { apiUrl } from "./base.js";

export async function getMe(expand = DEFAULT_EXPAND) {
// Check that the user is logged in via cookies
if (cookiesEnabled && !getCsrfToken()) return null;
// Check that the user is logged in via network request
const { status, data } = await session.get(
queryBuilder(apiUrl(`users/me/`), { expand }),
);
Expand Down
11 changes: 11 additions & 0 deletions src/common/dialog/stories/RevisionsDialog.stories.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,14 @@
msw: { handlers: [revisionControl.loading, mockGetMe.loading] },
}}
/>
<Story
name="Without CSRF Token"
{args}
parameters={{
msw: { handlers: [revisionControl.success, mockGetMe.data] },
cookie: {
csrftoken: "",
},
cookiePreserve: false,
}}
/>

0 comments on commit 84edb5e

Please sign in to comment.