|
17 | 17 | // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
18 | 18 | // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
19 | 19 | // SOFTWARE. |
20 | | -$(document).ready(function () { |
21 | | - $('#close_reload').click(function () { |
22 | | - location.reload(); |
23 | | - }); |
24 | | - $('#do_2fa').submit(function (event) { |
25 | | - event.preventDefault(); |
26 | 20 |
|
27 | | - $.ajax({ |
28 | | - type: 'PUT', |
29 | | - url: Router.route('account.security.totp'), |
30 | | - headers: { |
31 | | - 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content'), |
32 | | - } |
33 | | - }).done(function (data) { |
34 | | - var image = new Image(); |
35 | | - image.src = data.qrImage; |
36 | | - $(image).load(function () { |
37 | | - $('#hide_img_load').slideUp(function () { |
38 | | - $('#qr_image_insert').attr('src', image.src).slideDown(); |
| 21 | +var TwoFactorModal = (function () { |
| 22 | + |
| 23 | + function bindListeners() { |
| 24 | + $(document).ready(function () { |
| 25 | + $('#close_reload').click(function () { |
| 26 | + location.reload(); |
| 27 | + }); |
| 28 | + $('#do_2fa').submit(function (event) { |
| 29 | + event.preventDefault(); |
| 30 | + |
| 31 | + $.ajax({ |
| 32 | + type: 'PUT', |
| 33 | + url: Router.route('account.security.totp'), |
| 34 | + headers: { |
| 35 | + 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content'), |
| 36 | + } |
| 37 | + }).done(function (data) { |
| 38 | + var image = new Image(); |
| 39 | + image.src = data.qrImage; |
| 40 | + $(image).load(function () { |
| 41 | + $('#hide_img_load').slideUp(function () { |
| 42 | + $('#qr_image_insert').attr('src', image.src).slideDown(); |
| 43 | + }); |
| 44 | + }); |
| 45 | + $('#2fa_secret_insert').html(data.secret); |
| 46 | + $('#open2fa').modal('show'); |
| 47 | + }).fail(function (jqXHR) { |
| 48 | + alert('An error occured while attempting to load the 2FA setup modal. Please try again.'); |
| 49 | + console.error(jqXHR); |
| 50 | + }); |
| 51 | + |
| 52 | + }); |
| 53 | + $('#2fa_token_verify').submit(function (event) { |
| 54 | + event.preventDefault(); |
| 55 | + $('#submit_action').html('<i class="fa fa-spinner fa-spin"></i> Submit').addClass('disabled'); |
| 56 | + |
| 57 | + $.ajax({ |
| 58 | + type: 'POST', |
| 59 | + url: Router.route('account.security.totp'), |
| 60 | + headers: { |
| 61 | + 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content'), |
| 62 | + }, |
| 63 | + data: { |
| 64 | + token: $('#2fa_token').val() |
| 65 | + } |
| 66 | + }).done(function (data) { |
| 67 | + $('#notice_box_2fa').hide(); |
| 68 | + if (data === 'true') { |
| 69 | + $('#notice_box_2fa').html('<div class="alert alert-success">2-Factor Authentication has been enabled on your account. Press \'Close\' below to reload the page.</div>').slideDown(); |
| 70 | + } else { |
| 71 | + $('#notice_box_2fa').html('<div class="alert alert-danger">The token provided was invalid.</div>').slideDown(); |
| 72 | + } |
| 73 | + }).fail(function (jqXHR) { |
| 74 | + $('#notice_box_2fa').html('<div class="alert alert-danger">There was an error while attempting to enable 2-Factor Authentication on this account.</div>').slideDown(); |
| 75 | + console.error(jqXHR); |
| 76 | + }).always(function () { |
| 77 | + $('#submit_action').html('Submit').removeClass('disabled'); |
39 | 78 | }); |
40 | 79 | }); |
41 | | - $('#2fa_secret_insert').html(data.secret); |
42 | | - $('#open2fa').modal('show'); |
43 | | - }).fail(function (jqXHR) { |
44 | | - alert('An error occured while attempting to load the 2FA setup modal. Please try again.'); |
45 | | - console.error(jqXHR); |
46 | 80 | }); |
| 81 | + } |
47 | 82 |
|
48 | | - }); |
49 | | - $('#2fa_token_verify').submit(function (event) { |
50 | | - event.preventDefault(); |
51 | | - $('#submit_action').html('<i class="fa fa-spinner fa-spin"></i> Submit').addClass('disabled'); |
| 83 | + return { |
| 84 | + init: function () { |
| 85 | + bindListeners(); |
| 86 | + } |
| 87 | + } |
52 | 88 |
|
53 | | - $.ajax({ |
54 | | - type: 'POST', |
55 | | - url: Router.route('account.security.totp'), |
56 | | - headers: { |
57 | | - 'X-CSRF-TOKEN': $('meta[name="_token"]').attr('content'), |
58 | | - }, |
59 | | - data: { |
60 | | - token: $('#2fa_token').val() |
61 | | - } |
62 | | - }).done(function (data) { |
63 | | - $('#notice_box_2fa').hide(); |
64 | | - if (data === 'true') { |
65 | | - $('#notice_box_2fa').html('<div class="alert alert-success">2-Factor Authentication has been enabled on your account. Press \'Close\' below to reload the page.</div>').slideDown(); |
66 | | - } else { |
67 | | - $('#notice_box_2fa').html('<div class="alert alert-danger">The token provided was invalid.</div>').slideDown(); |
68 | | - } |
69 | | - }).fail(function (jqXHR) { |
70 | | - $('#notice_box_2fa').html('<div class="alert alert-danger">There was an error while attempting to enable 2-Factor Authentication on this account.</div>').slideDown(); |
71 | | - console.error(jqXHR); |
72 | | - }).always(function () { |
73 | | - $('#submit_action').html('Submit').removeClass('disabled'); |
74 | | - }); |
75 | | - }); |
76 | 89 | }); |
| 90 | + |
| 91 | +TwoFactorModal.init(); |
0 commit comments