about summary refs log tree commit diff
path: root/app/javascript/flavours/glitch/util/log_out.js
blob: 42dcee03ecbc3813d77d1c3f40e8575b73d2f975 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import Rails from '@rails/ujs';
import { signOutLink } from 'flavours/glitch/util/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();
};