about summary refs log tree commit diff
path: root/app/javascript/flavours/glitch/utils/log_out.js
diff options
context:
space:
mode:
Diffstat (limited to 'app/javascript/flavours/glitch/utils/log_out.js')
-rw-r--r--app/javascript/flavours/glitch/utils/log_out.js34
1 files changed, 34 insertions, 0 deletions
diff --git a/app/javascript/flavours/glitch/utils/log_out.js b/app/javascript/flavours/glitch/utils/log_out.js
new file mode 100644
index 000000000..f82041150
--- /dev/null
+++ b/app/javascript/flavours/glitch/utils/log_out.js
@@ -0,0 +1,34 @@
+import Rails from '@rails/ujs';
+import { signOutLink } from 'flavours/glitch/utils/backend_links';
+
+export const logOut = () => {
+  const form = document.createElement('form');
+
+  const methodInput = document.createElement('input');
+  methodInput.setAttribute('name', '_method');
+  methodInput.setAttribute('value', 'delete');
+  methodInput.setAttribute('type', 'hidden');
+  form.appendChild(methodInput);
+
+  const csrfToken = Rails.csrfToken();
+  const csrfParam = Rails.csrfParam();
+
+  if (csrfParam && csrfToken) {
+    const csrfInput = document.createElement('input');
+    csrfInput.setAttribute('name', csrfParam);
+    csrfInput.setAttribute('value', csrfToken);
+    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();
+};