Skip to content

Commit

Permalink
Added integrity token to signup-form package
Browse files Browse the repository at this point in the history
ref KTLO-1
  • Loading branch information
sam-lord committed Aug 22, 2024
1 parent ef4f793 commit ebc8700
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
7 changes: 7 additions & 0 deletions apps/signup-form/src/Preview.stories.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ const Preview: React.FC<SignupFormOptions & {
}

return;
},
getIntegrityToken: async () => {
await new Promise((resolve) => {
setTimeout(resolve, 500);
});

return 'testtoken';
}
},
t: i18n.t,
Expand Down
3 changes: 2 additions & 1 deletion apps/signup-form/src/components/pages/FormPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ export const FormPage: React.FC = () => {
setLoading(true);

try {
await api.sendMagicLink({email, labels: options.labels});
const integrityToken = await api.getIntegrityToken();
await api.sendMagicLink({email, labels: options.labels, integrityToken});

if (minimal) {
// Don't go to the success page, but show the success state in the form
Expand Down
21 changes: 19 additions & 2 deletions apps/signup-form/src/utils/api.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,31 @@ export const setupGhostApi = ({siteUrl}: {siteUrl: string}) => {
}

return {
sendMagicLink: async ({email, labels}: {email: string, labels: string[]}) => {
getIntegrityToken: async (): Promise<string> => {
const url = endpointFor({type: 'members', resource: 'integrity-token'});

const response = await fetch(url, {
headers: {
'app-pragma': 'no-cache',
'x-ghost-version': '5.90'
}
});

if (response.status < 200 || response.status >= 300) {
throw new Error(response.statusText);
}

return response.text();
},
sendMagicLink: async ({email, integrityToken, labels}: {email: string, labels: string[], integrityToken: string}) => {
const url = endpointFor({type: 'members', resource: 'send-magic-link'});

const payload = JSON.stringify({
email,
emailType: 'signup',
labels,
urlHistory: getUrlHistory({siteUrl})
urlHistory: getUrlHistory({siteUrl}),
integrityToken
});

const response = await fetch(url, {
Expand Down
4 changes: 4 additions & 0 deletions apps/signup-form/test/utils/e2e.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,9 @@ export async function mockApi({page, status = 200}: {page: any, status?: number}
await route.abort('addressunreachable');
});

await page.route(`${MOCKED_SITE_URL}/members/api/integrity-token/`, async (route) => {
await route.fulfill('testtoken');
});

return lastApiRequest;
}

0 comments on commit ebc8700

Please sign in to comment.