From 09929a42f7963cfac825e833dfa9ffe4b70c7d9a Mon Sep 17 00:00:00 2001 From: Renaud Chaput Date: Tue, 13 Aug 2024 19:49:23 +0200 Subject: [PATCH] [Glitch] Fix log out from user menu not working on Safari Port c3e1d86d589d4f54df6d5b2752822aa7b577bf29 to glitch-soc Signed-off-by: Claire --- .../confirmation_modals/log_out.tsx | 2 +- .../flavours/glitch/utils/log_out.ts | 52 ++++++------------- 2 files changed, 18 insertions(+), 36 deletions(-) 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(); -}; +}