[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
parent
a190efcd6d
commit
09929a42f7
|
@ -25,7 +25,7 @@ export const ConfirmLogOutModal: React.FC<BaseConfirmationModalProps> = ({
|
||||||
const intl = useIntl();
|
const intl = useIntl();
|
||||||
|
|
||||||
const onConfirm = useCallback(() => {
|
const onConfirm = useCallback(() => {
|
||||||
logOut();
|
void logOut();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
|
|
|
@ -1,38 +1,20 @@
|
||||||
import { signOutLink } from 'flavours/glitch/utils/backend_links';
|
import api from 'flavours/glitch/api';
|
||||||
|
|
||||||
export const logOut = () => {
|
export async function logOut() {
|
||||||
const form = document.createElement('form');
|
try {
|
||||||
|
const response = await api(false).delete<{ redirect_to?: string }>(
|
||||||
|
'/auth/sign_out',
|
||||||
|
{ headers: { Accept: 'application/json' }, withCredentials: true },
|
||||||
|
);
|
||||||
|
|
||||||
const methodInput = document.createElement('input');
|
if (response.status === 200 && response.data.redirect_to)
|
||||||
methodInput.setAttribute('name', '_method');
|
window.location.href = response.data.redirect_to;
|
||||||
methodInput.setAttribute('value', 'delete');
|
else
|
||||||
methodInput.setAttribute('type', 'hidden');
|
console.error(
|
||||||
form.appendChild(methodInput);
|
'Failed to log out, got an unexpected non-redirect response from the server',
|
||||||
|
response,
|
||||||
const csrfToken = document.querySelector<HTMLMetaElement>(
|
);
|
||||||
'meta[name=csrf-token]',
|
} catch (error) {
|
||||||
);
|
console.error('Failed to log out, response was an error', error);
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
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();
|
|
||||||
};
|
|
||||||
|
|
Loading…
Reference in New Issue