344b043a36e5917bcc8bbe55b7c720d39402db41
[project/luci.git] / themes / luci-theme-bootstrap / htdocs / luci-static / resources / view / bootstrap / sysauth.js
1 'use strict';
2 'require ui';
3 'require dom';
4 'require form';
5 'require view';
6 'require request';
7
8 var data = { login: {} };
9
10 return view.extend({
11 load: function() {
12 var m, s, o;
13
14 m = new form.JSONMap(data);
15 s = m.section(form.NamedSection, 'login');
16
17 o = s.option(form.Value, 'username', _('Username'));
18 o.default = L.env.default_login_user;
19
20 o = s.option(form.Value, 'password', _('Password'));
21 o.password = true;
22 o.validate = function(section_id, value) {
23 var msg = document.querySelector('alert-message');
24
25 if (msg && value.length)
26 msg.parentNode.removeChild(msg);
27
28 return true;
29 };
30
31 return m.render();
32 },
33
34 render: function(form) {
35 ui.showModal(_('Authorization Required'), [
36 form,
37 E('hr'),
38 E('div', { 'class': 'alert-message error hidden' }, [
39 _('Invalid username and/or password! Please try again.')
40 ]),
41 E('button', {
42 'class': 'btn cbi-button-positive important',
43 'click': ui.createHandlerFn(this, 'handleLogin', form)
44 }, [ _('Login') ])
45 ], 'login');
46
47 document.querySelector('[id="widget.cbid.json.login.password"]').focus();
48
49 form.addEventListener('keyup', L.bind(function(form, ev) {
50 if (ev.key === 'Enter' || ev.keyCode === 13)
51 document.querySelector('.cbi-button-positive.important').click();
52 }, this, form));
53
54 return E('div', { 'class': 'spinning' }, _('Loading view…'));
55 },
56
57 handleLoginError: function(err) {
58 document.querySelectorAll('.alert-message.error').forEach(function(msg) {
59 msg.firstChild.data = _('The login request failed with error: %h').format(err.message);
60 msg.classList.remove('hidden');
61 msg.classList.add('flash');
62 });
63 },
64
65 handleLoginReply: function(res) {
66 if (res.status != 403) {
67 ui.hideModal();
68 location.reload();
69
70 return;
71 }
72
73 document.querySelectorAll('.alert-message.error').forEach(function(msg) {
74 msg.firstChild.data = _('Invalid username and/or password! Please try again.');
75 msg.classList.remove('hidden');
76 msg.classList.add('flash');
77 });
78 },
79
80 handleLogin: function(form, ev) {
81 var fd = new FormData();
82
83 document.querySelectorAll('.alert-message.error').forEach(function(msg) {
84 msg.classList.add('hidden');
85 msg.classList.remove('flash');
86 });
87
88 dom.callClassMethod(form, 'save');
89
90 fd.append('luci_username', data.login.username != null ? data.login.username : '');
91 fd.append('luci_password', data.login.password != null ? data.login.password : '');
92
93 Object.getPrototypeOf(L).notifySessionExpiry = function() {};
94
95 return request.post(location.href, fd).then(this.handleLoginReply, this.handleLoginError);
96 },
97
98 addFooter: function() {}
99 });