[Glitch] Fix log out from user menu not working on Safari

Port c3e1d86d58 to glitch-soc

Signed-off-by: Claire <claire.github-309c@sitedethib.com>
pull/2813/head
Renaud Chaput 2024-08-13 19:49:23 +02:00 committed by Claire
parent a190efcd6d
commit 09929a42f7
2 changed files with 18 additions and 36 deletions

View File

@ -25,7 +25,7 @@ export const ConfirmLogOutModal: React.FC<BaseConfirmationModalProps> = ({
const intl = useIntl();
const onConfirm = useCallback(() => {
logOut();
void logOut();
}, []);
return (

View File

@ -1,38 +1,20 @@
import { signOutLink } from 'flavours/glitch/utils/backend_links';
import api from 'flavours/glitch/api';
export const logOut = () => {
const form = document.createElement('form');
export async function logOut() {
try {
const response = await api(false).delete<{ redirect_to?: string }>(
'/auth/sign_out',
{ headers: { Accept: 'application/json' }, withCredentials: true },
);
const methodInput = document.createElement('input');
methodInput.setAttribute('name', '_method');
methodInput.setAttribute('value', 'delete');
methodInput.setAttribute('type', 'hidden');
form.appendChild(methodInput);
const csrfToken = document.querySelector<HTMLMetaElement>(
'meta[name=csrf-token]',
);
const csrfParam = document.querySelector<HTMLMetaElement>(
'meta[name=csrf-param]',
);
if (csrfParam && csrfToken) {
const csrfInput = document.createElement('input');
csrfInput.setAttribute('name', csrfParam.content);
csrfInput.setAttribute('value', csrfToken.content);
csrfInput.setAttribute('type', 'hidden');
form.appendChild(csrfInput);
if (response.status === 200 && response.data.redirect_to)
window.location.href = response.data.redirect_to;
else
console.error(
'Failed to log out, got an unexpected non-redirect response from the server',
response,
);
} catch (error) {
console.error('Failed to log out, response was an error', error);
}
const submitButton = document.createElement('input');
submitButton.setAttribute('type', 'submit');
form.appendChild(submitButton);
form.method = 'post';
form.action = signOutLink;
form.style.display = 'none';
document.body.appendChild(form);
submitButton.click();
};
}