diff --git a/app/javascript/flavours/glitch/features/ui/components/confirmation_modals/log_out.tsx b/app/javascript/flavours/glitch/features/ui/components/confirmation_modals/log_out.tsx index ac9439461f..9c90e0f8b9 100644 --- a/app/javascript/flavours/glitch/features/ui/components/confirmation_modals/log_out.tsx +++ b/app/javascript/flavours/glitch/features/ui/components/confirmation_modals/log_out.tsx @@ -25,7 +25,7 @@ export const ConfirmLogOutModal: React.FC = ({ const intl = useIntl(); const onConfirm = useCallback(() => { - logOut(); + void logOut(); }, []); return ( diff --git a/app/javascript/flavours/glitch/utils/log_out.ts b/app/javascript/flavours/glitch/utils/log_out.ts index d4db471a6e..a399fa4d0a 100644 --- a/app/javascript/flavours/glitch/utils/log_out.ts +++ b/app/javascript/flavours/glitch/utils/log_out.ts @@ -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( - 'meta[name=csrf-token]', - ); - - const csrfParam = document.querySelector( - '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(); -}; +}