2fa.js 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. import $ from 'jquery';
  2. import { config } from 'grav-config';
  3. import request from '../utils/request';
  4. const body = $('body');
  5. // Dashboard update and Grav update
  6. body.on('click', '[data-2fa-regenerate]', function(event) {
  7. event.preventDefault();
  8. let element = $(this);
  9. let url = `${config.base_url_relative}/ajax.json/task${config.param_sep}regenerate2FASecret`;
  10. element.attr('disabled', 'disabled').find('> .fa').addClass('fa-spin');
  11. request(url, { method: 'post' }, (response) => {
  12. $('[data-2fa-image]').attr('src', response.image);
  13. $('[data-2fa-secret]').text(response.secret);
  14. $('[data-2fa-value]').val(response.secret.replace(' ', ''));
  15. element.removeAttr('disabled').find('> .fa').removeClass('fa-spin');
  16. });
  17. });
  18. const toggleSecret = () => {
  19. const toggle = $('#toggle_twofa_enabled1');
  20. const secret = $('.twofa-secret');
  21. secret[toggle.is(':checked') ? 'addClass' : 'removeClass']('show');
  22. };
  23. body.on('click', '.twofa-toggle input', toggleSecret);
  24. toggleSecret();