Skip to content

Commit

Permalink
Fix "delete me" feature (#326)
Browse files Browse the repository at this point in the history
* wip: fix "delete me" feature

* minor fixes
  • Loading branch information
Reeywhaar authored and umputun committed May 13, 2019
1 parent 7d01165 commit 53f2f82
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 22 deletions.
2 changes: 2 additions & 0 deletions frontend/app/common/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -145,11 +145,13 @@ export const deleteMe = (): Promise<{
}> =>
fetcher.post({
url: `/deleteme?site=${siteId}`,
withCredentials: true,
});

export const approveDeleteMe = (token: string): Promise<void> =>
fetcher.get({
url: `/admin/deleteme?token=${token}`,
withCredentials: true,
});

/* admin */
Expand Down
46 changes: 24 additions & 22 deletions frontend/app/deleteme.ts
Original file line number Diff line number Diff line change
@@ -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);
Expand All @@ -11,7 +12,7 @@ if (document.readyState === 'loading') {
}

async function init(): Promise<void> {
__webpack_public_path__ = BASE_URL + '/web/';
__webpack_public_path__ = window.location.origin + '/web/';

await loadPolyfills();

Expand All @@ -22,34 +23,35 @@ async function init(): Promise<void> {
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 = `
<h3>User deleted successfully</h3>
<pre>${JSON.stringify(data, null, 4)}</pre>
`;
},
err => {
node.innerHTML = `
<h3>Something went wrong</h3>
<pre>${err}</pre>
`;
}
);
})
.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 = `
<h3>Something went wrong</h3>
<pre>${message}</pre>
`;
}
);
});
}

function handleNotAuthorizedError(node: HTMLElement): void {
node.innerHTML = `
<h3>You are not logged in</h3>
<p><a href='${BASE_URL}' target='_blank'>Sign in</a> as admin to delete user information</p>
<p><a href='/web' target='_blank'>Sign in</a> as admin to delete user information</p>
`;
}

0 comments on commit 53f2f82

Please sign in to comment.