diff --git a/frontend/app/common/api.ts b/frontend/app/common/api.ts index dbe1b960dc..010e97c1a4 100644 --- a/frontend/app/common/api.ts +++ b/frontend/app/common/api.ts @@ -145,11 +145,13 @@ export const deleteMe = (): Promise<{ }> => fetcher.post({ url: `/deleteme?site=${siteId}`, + withCredentials: true, }); export const approveDeleteMe = (token: string): Promise => fetcher.get({ url: `/admin/deleteme?token=${token}`, + withCredentials: true, }); /* admin */ diff --git a/frontend/app/deleteme.ts b/frontend/app/deleteme.ts index 489c00dfe6..89360eb18d 100644 --- a/frontend/app/deleteme.ts +++ b/frontend/app/deleteme.ts @@ -1,8 +1,9 @@ /* eslint-disable no-console */ import loadPolyfills from '@app/common/polyfills'; -import { NODE_ID, BASE_URL } from '@app/common/constants'; +import { NODE_ID } from '@app/common/constants'; import { approveDeleteMe, getUser } from '@app/common/api'; import { token } from '@app/common/settings'; +import { ApiError } from './common/types'; if (document.readyState === 'loading') { document.addEventListener('DOMContentLoaded', init); @@ -11,7 +12,7 @@ if (document.readyState === 'loading') { } async function init(): Promise { - __webpack_public_path__ = BASE_URL + '/web/'; + __webpack_public_path__ = window.location.origin + '/web/'; await loadPolyfills(); @@ -22,34 +23,35 @@ async function init(): Promise { return; } - getUser() - .then(user => { - if (user && !user.admin) { - handleNotAuthorizedError(node); - return; - } + getUser().then(user => { + if (!user || !user.admin) { + handleNotAuthorizedError(node); + return; + } - approveDeleteMe(token!).then( - data => { - node.innerHTML = ` + approveDeleteMe(token!).then( + data => { + node.innerHTML = `

User deleted successfully

${JSON.stringify(data, null, 4)}
`; - }, - err => { - node.innerHTML = ` -

Something went wrong

-
${err}
- `; - } - ); - }) - .catch(() => handleNotAuthorizedError(node)); + }, + (err: Error | ApiError | string) => { + const message = + err instanceof Error ? err.message : typeof err === 'object' && err !== null && err.error ? err.error : err; + console.error(err); + node.innerHTML = ` +

Something went wrong

+
${message}
+ `; + } + ); + }); } function handleNotAuthorizedError(node: HTMLElement): void { node.innerHTML = `

You are not logged in

-

Sign in as admin to delete user information

+

Sign in as admin to delete user information

`; }