jwtbutler 1.6.8 → 1.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,499 +1,350 @@
1
1
  "use strict";
2
2
 
3
3
  var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
4
-
5
4
  Object.defineProperty(exports, "__esModule", {
6
5
  value: true
7
6
  });
8
7
  exports.default = void 0;
9
-
10
- require("core-js/modules/es.string.sub.js");
11
-
12
- require("core-js/modules/es.regexp.exec.js");
13
-
14
- require("core-js/modules/es.string.split.js");
15
-
16
- require("core-js/modules/es.object.to-string.js");
17
-
18
- require("core-js/modules/es.promise.js");
19
-
20
- require("core-js/modules/es.regexp.to-string.js");
21
-
22
- require("core-js/modules/web.dom-collections.for-each.js");
23
-
24
- require("core-js/modules/es.array.concat.js");
25
-
26
- var _defineProperty2 = _interopRequireDefault(require("@babel/runtime/helpers/defineProperty"));
27
-
28
- var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
29
-
30
- var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
31
-
8
+ require("core-js/modules/es.json.stringify.js");
9
+ require("core-js/modules/esnext.iterator.constructor.js");
10
+ require("core-js/modules/esnext.iterator.for-each.js");
32
11
  require("whatwg-fetch");
33
-
34
12
  var _helpers = _interopRequireDefault(require("./_helpers"));
35
-
36
13
  // use fetch
37
- var jwtbutler = /*#__PURE__*/function () {
38
- function jwtbutler(config) {
39
- (0, _classCallCheck2.default)(this, jwtbutler);
40
14
 
15
+ class jwtbutler {
16
+ constructor(config) {
41
17
  if (!('auth_login' in config)) {
42
18
  config.auth_login = 'email';
43
19
  }
44
-
45
20
  this.config = config;
46
21
  }
47
-
48
- (0, _createClass2.default)(jwtbutler, [{
49
- key: "isLoggedIn",
50
- value: function isLoggedIn() {
51
- if (this.getPayload() === null) {
52
- return false;
53
- }
54
-
55
- return true;
22
+ isLoggedIn() {
23
+ if (this.getPayload() === null) {
24
+ return false;
56
25
  }
57
- }, {
58
- key: "getUserId",
59
- value: function getUserId() {
60
- var payload = this.getPayload();
61
-
62
- if (payload === null) {
63
- return null;
64
- }
65
-
66
- return payload.sub;
26
+ return true;
27
+ }
28
+ getUserId() {
29
+ let payload = this.getPayload();
30
+ if (payload === null) {
31
+ return null;
67
32
  }
68
- }, {
69
- key: "getPayload",
70
- value: function getPayload() {
71
- if (_helpers.default.cookieGet('access_token') === null) {
72
- return null;
73
- }
74
-
75
- try {
76
- return JSON.parse(atob(_helpers.default.cookieGet('access_token').split('.')[1]));
77
- } catch (e) {
78
- return null;
79
- }
33
+ return payload.sub;
34
+ }
35
+ getPayload() {
36
+ if (_helpers.default.cookieGet('access_token') === null) {
37
+ return null;
80
38
  }
81
- }, {
82
- key: "logout",
83
- value: function logout() {
84
- var _this2 = this;
85
-
86
- return new Promise(function (resolve, reject) {
87
- _this2.addLoadingState('logging-out');
88
-
89
- fetch(_this2.config.auth_server + '/logout', {
39
+ try {
40
+ return JSON.parse(atob(_helpers.default.cookieGet('access_token').split('.')[1]));
41
+ } catch (e) {
42
+ return null;
43
+ }
44
+ }
45
+ logout() {
46
+ return new Promise((resolve, reject) => {
47
+ this.addLoadingState('logging-out');
48
+ fetch(this.config.auth_server + '/logout', {
49
+ method: 'POST',
50
+ headers: {
51
+ 'content-type': 'application/json',
52
+ Authorization: 'Bearer ' + _helpers.default.cookieGet('access_token')
53
+ },
54
+ cache: 'no-cache'
55
+ }).then(res => res.json()).catch(err => err).then(response => {
56
+ this.setCookies(null).then(() => {
57
+ this.removeLoadingStates();
58
+ resolve();
59
+ }).catch(error => {
60
+ reject(error);
61
+ });
62
+ });
63
+ });
64
+ }
65
+ login() {
66
+ return new Promise((resolve, reject) => {
67
+ if (_helpers.default.cookieGet('access_token') !== null) {
68
+ this.addLoadingState('logging-in');
69
+ fetch(this.config.auth_server + '/check', {
90
70
  method: 'POST',
71
+ body: JSON.stringify({
72
+ access_token: _helpers.default.cookieGet('access_token')
73
+ }),
91
74
  headers: {
92
- 'content-type': 'application/json',
93
- Authorization: 'Bearer ' + _helpers.default.cookieGet('access_token')
75
+ 'content-type': 'application/json'
94
76
  },
95
77
  cache: 'no-cache'
96
- }).then(function (res) {
97
- return res.json();
98
- }).catch(function (err) {
99
- return err;
100
- }).then(function (response) {
101
- _this2.setCookies(null).then(function () {
102
- _this2.removeLoadingStates();
103
-
104
- resolve();
105
- }).catch(function (error) {
106
- reject(error);
107
- });
108
- });
109
- });
110
- }
111
- }, {
112
- key: "login",
113
- value: function login() {
114
- var _this3 = this;
115
-
116
- return new Promise(function (resolve, reject) {
117
- if (_helpers.default.cookieGet('access_token') !== null) {
118
- _this3.addLoadingState('logging-in');
119
-
120
- fetch(_this3.config.auth_server + '/check', {
121
- method: 'POST',
122
- body: JSON.stringify({
123
- access_token: _helpers.default.cookieGet('access_token')
124
- }),
125
- headers: {
126
- 'content-type': 'application/json'
127
- },
128
- cache: 'no-cache'
129
- }).then(function (res) {
130
- return res.json();
131
- }).catch(function (err) {
132
- return err;
133
- }).then(function (response) {
134
- if (response.success === true) {
135
- _this3.setCookies(_helpers.default.cookieGet('access_token')).then(function () {
136
- _this3.removeLoadingStates();
137
-
138
- resolve();
139
- }).catch(function (error) {
140
- reject(error);
141
- });
142
- } else {
143
- fetch(_this3.config.auth_server + '/refresh', {
144
- method: 'POST',
145
- headers: {
146
- 'content-type': 'application/json',
147
- Authorization: 'Bearer ' + _helpers.default.cookieGet('access_token')
148
- },
149
- cache: 'no-cache'
150
- }).then(function (res) {
151
- return res.json();
152
- }).catch(function (err) {
153
- return err;
154
- }).then(function (response) {
155
- if (response.success === true) {
156
- _this3.setCookies(response.data.access_token).then(function () {
157
- _this3.removeLoadingStates();
158
-
159
- resolve();
160
- }).catch(function (error) {
161
- reject(error);
162
- });
163
- } else {
164
- _this3.renderLoginFormWithPromise().then(function () {
165
- resolve();
166
- });
167
- }
168
- });
169
- }
170
- });
171
- } else {
172
- _this3.renderLoginFormWithPromise().then(function () {
173
- resolve();
174
- }).catch(function () {
175
- reject();
176
- });
177
- }
178
- });
179
- }
180
- }, {
181
- key: "fetch",
182
- value: function (_fetch) {
183
- function fetch(_x) {
184
- return _fetch.apply(this, arguments);
185
- }
186
-
187
- fetch.toString = function () {
188
- return _fetch.toString();
189
- };
190
-
191
- return fetch;
192
- }(function (url) {
193
- var _this4 = this;
194
-
195
- var args = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
196
- return new Promise(function (resolve, reject) {
197
- if (!('headers' in args)) {
198
- args.headers = {};
199
- }
200
-
201
- if (!('tries' in args)) {
202
- args.tries = 0;
203
- }
204
-
205
- args.tries++;
206
-
207
- if (args.tries > 3) {
208
- reject(null);
209
- return;
210
- } else if (_this4.isLoggedIn() === false) {
211
- _this4.login().then(function () {
212
- _this4.fetch(url, args).then(function (response) {
213
- resolve(response);
214
- }).catch(function (error) {
78
+ }).then(res => res.json()).catch(err => err).then(response => {
79
+ if (response.success === true) {
80
+ this.setCookies(_helpers.default.cookieGet('access_token')).then(() => {
81
+ this.removeLoadingStates();
82
+ resolve();
83
+ }).catch(error => {
215
84
  reject(error);
216
85
  });
217
- }).catch(function (error) {
86
+ } else {
87
+ fetch(this.config.auth_server + '/refresh', {
88
+ method: 'POST',
89
+ headers: {
90
+ 'content-type': 'application/json',
91
+ Authorization: 'Bearer ' + _helpers.default.cookieGet('access_token')
92
+ },
93
+ cache: 'no-cache'
94
+ }).then(res => res.json()).catch(err => err).then(response => {
95
+ if (response.success === true) {
96
+ this.setCookies(response.data.access_token).then(() => {
97
+ this.removeLoadingStates();
98
+ resolve();
99
+ }).catch(error => {
100
+ reject(error);
101
+ });
102
+ } else {
103
+ this.renderLoginFormWithPromise().then(() => {
104
+ resolve();
105
+ });
106
+ }
107
+ });
108
+ }
109
+ });
110
+ } else {
111
+ this.renderLoginFormWithPromise().then(() => {
112
+ resolve();
113
+ }).catch(() => {
114
+ reject();
115
+ });
116
+ }
117
+ });
118
+ }
119
+ fetch(url) {
120
+ let args = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
121
+ return new Promise((resolve, reject) => {
122
+ if (!('headers' in args)) {
123
+ args.headers = {};
124
+ }
125
+ if (!('tries' in args)) {
126
+ args.tries = 0;
127
+ }
128
+ args.tries++;
129
+ if (args.tries > 3) {
130
+ reject(null);
131
+ return;
132
+ } else if (this.isLoggedIn() === false) {
133
+ this.login().then(() => {
134
+ this.fetch(url, args).then(response => {
135
+ resolve(response);
136
+ }).catch(error => {
218
137
  reject(error);
219
138
  });
220
- } else {
221
- _this4.addLoadingState('fetching');
222
-
223
- args.headers.Authorization = 'Bearer ' + _helpers.default.cookieGet('access_token');
224
- fetch(url, args).then(function (v) {
225
- return v;
226
- }).catch(function (v) {
227
- return v;
228
- }).then(function (response) {
229
- _this4.removeLoadingStates();
230
-
231
- if (response.status === 401) {
232
- _this4.addLoadingState('logging-in');
233
-
234
- fetch(_this4.config.auth_server + '/refresh', {
235
- method: 'POST',
236
- headers: {
237
- 'content-type': 'application/json',
238
- Authorization: 'Bearer ' + _helpers.default.cookieGet('access_token')
239
- },
240
- cache: 'no-cache'
241
- }).then(function (res) {
242
- return res.json();
243
- }).catch(function (error) {
244
- return error;
245
- }).then(function (response) {
246
- if (response.success === true) {
247
- _this4.setCookies(response.data.access_token).then(function () {
248
- _this4.removeLoadingStates();
249
-
250
- _this4.fetch(url, args).then(function (response) {
251
- resolve(response);
252
- }).catch(function (error) {
253
- reject(error);
254
- });
255
- }).catch(function (error) {
256
- _this4.removeLoadingStates();
257
-
139
+ }).catch(error => {
140
+ reject(error);
141
+ });
142
+ } else {
143
+ this.addLoadingState('fetching');
144
+ args.headers.Authorization = 'Bearer ' + _helpers.default.cookieGet('access_token');
145
+ fetch(url, args).then(v => v).catch(v => v).then(response => {
146
+ this.removeLoadingStates();
147
+ if (response.status === 401) {
148
+ this.addLoadingState('logging-in');
149
+ fetch(this.config.auth_server + '/refresh', {
150
+ method: 'POST',
151
+ headers: {
152
+ 'content-type': 'application/json',
153
+ Authorization: 'Bearer ' + _helpers.default.cookieGet('access_token')
154
+ },
155
+ cache: 'no-cache'
156
+ }).then(res => res.json()).catch(error => error).then(response => {
157
+ if (response.success === true) {
158
+ this.setCookies(response.data.access_token).then(() => {
159
+ this.removeLoadingStates();
160
+ this.fetch(url, args).then(response => {
161
+ resolve(response);
162
+ }).catch(error => {
258
163
  reject(error);
259
164
  });
260
- } else {
261
- _this4.removeLoadingStates();
262
-
263
- _this4.renderLoginFormWithPromise().then(function () {
264
- _this4.fetch(url, args).then(function (response) {
265
- resolve(response);
266
- }).catch(function (error) {
267
- reject(error);
268
- });
269
- }).catch(function (error) {
165
+ }).catch(error => {
166
+ this.removeLoadingStates();
167
+ reject(error);
168
+ });
169
+ } else {
170
+ this.removeLoadingStates();
171
+ this.renderLoginFormWithPromise().then(() => {
172
+ this.fetch(url, args).then(response => {
173
+ resolve(response);
174
+ }).catch(error => {
270
175
  reject(error);
271
176
  });
272
- }
273
- });
274
- } else {
275
- resolve(response);
276
- }
277
- });
278
- }
279
- });
280
- })
281
- }, {
282
- key: "setCookies",
283
- value: function setCookies() {
284
- var _this5 = this;
285
-
286
- var access_token = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
287
- return new Promise(function (resolve, reject) {
288
- if (_this5.setCookieLoading === undefined) {
289
- _this5.setCookieLoading === false;
290
- }
291
-
292
- if (_this5.setCookieLoading === true) {
293
- resolve();
294
- return;
295
- }
296
-
297
- _this5.setCookieLoading = true;
298
-
299
- if (access_token !== null) {
300
- _helpers.default.cookieSet('access_token', access_token, 28);
301
- } else {
302
- _helpers.default.cookieDelete('access_token');
303
- }
304
-
305
- if (_this5.config.sso === undefined || _this5.config.sso.length === 1 && _this5.config.sso[0] === window.location.protocol + '//' + window.location.host) {
306
- _this5.setCookieLoading = false;
307
- resolve();
308
- return;
309
- }
310
-
311
- _helpers.default.remove(document.querySelector('.iframe_wrapper'));
312
-
313
- var iframe_wrapper = document.createElement('div');
314
- iframe_wrapper.setAttribute('class', 'iframe_wrapper');
315
- iframe_wrapper.style.position = 'absolute';
316
- iframe_wrapper.style.opacity = '0';
317
- document.body.appendChild(iframe_wrapper);
318
- var timeout = null;
319
-
320
- var _this = _this5,
321
- todo = _this5.config.sso.length - 1,
322
- waitForPostMessage = function waitForPostMessage(e) {
177
+ }).catch(error => {
178
+ reject(error);
179
+ });
180
+ }
181
+ });
182
+ } else {
183
+ resolve(response);
184
+ }
185
+ });
186
+ }
187
+ });
188
+ }
189
+ setCookies() {
190
+ let access_token = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : null;
191
+ return new Promise((resolve, reject) => {
192
+ if (this.setCookieLoading === undefined) {
193
+ this.setCookieLoading === false;
194
+ }
195
+ if (this.setCookieLoading === true) {
196
+ resolve();
197
+ return;
198
+ }
199
+ this.setCookieLoading = true;
200
+ if (access_token !== null) {
201
+ _helpers.default.cookieSet('access_token', access_token, 28);
202
+ } else {
203
+ _helpers.default.cookieDelete('access_token');
204
+ }
205
+ if (this.config.sso === undefined || this.config.sso.length === 1 && this.config.sso[0] === window.location.protocol + '//' + window.location.host) {
206
+ this.setCookieLoading = false;
207
+ resolve();
208
+ return;
209
+ }
210
+ _helpers.default.remove(document.querySelector('.iframe_wrapper'));
211
+ let iframe_wrapper = document.createElement('div');
212
+ iframe_wrapper.setAttribute('class', 'iframe_wrapper');
213
+ iframe_wrapper.style.position = 'absolute';
214
+ iframe_wrapper.style.opacity = '0';
215
+ document.body.appendChild(iframe_wrapper);
216
+ let timeout = null;
217
+ let _this = this,
218
+ todo = this.config.sso.length - 1,
219
+ waitForPostMessage = function (e) {
323
220
  if (_this.config.sso.indexOf(e.origin) === -1) {
324
221
  return;
325
222
  }
326
-
327
223
  if (e.data !== undefined && e.data !== null && 'success' in e.data && e.data.success === true) {
328
224
  todo--;
329
- } //console.log(todo);
330
-
331
-
225
+ }
226
+ //console.log(todo);
332
227
  if (todo <= 0) {
333
228
  if (timeout) {
334
229
  //console.log('cleared timeout');
335
230
  clearTimeout(timeout);
336
231
  }
337
-
338
232
  window.removeEventListener('message', waitForPostMessage, false);
339
-
340
233
  _helpers.default.remove(document.querySelector('.iframe_wrapper'));
341
-
342
234
  _this.setCookieLoading = false;
343
235
  resolve();
344
236
  }
345
237
  };
346
-
347
- window.addEventListener('message', waitForPostMessage, false);
348
- timeout = setTimeout(function () {
349
- if (_this5.setCookieLoading === true) {
350
- //console.log('timeout');
351
- window.removeEventListener('message', waitForPostMessage, false);
352
-
353
- _helpers.default.remove(document.querySelector('.iframe_wrapper'));
354
-
355
- _this5.setCookieLoading = false;
356
- resolve();
357
- }
358
- }, 20000);
359
-
360
- _this5.config.sso.forEach(function (sso__value) {
361
- if (sso__value === window.location.protocol + '//' + window.location.host) {
362
- return;
363
- }
364
-
365
- var iframe = document.createElement('iframe');
366
- iframe.setAttribute('src', sso__value + '/sso.html');
367
- iframe.style.width = '1px';
368
- iframe.style.height = '1px';
369
- iframe.addEventListener('load', function (e) {
370
- iframe.contentWindow.postMessage({
371
- access_token: access_token
372
- }, sso__value);
373
- });
374
- document.querySelector('.iframe_wrapper').appendChild(iframe);
375
- });
376
- });
377
- }
378
- }, {
379
- key: "renderLoginFormWithPromise",
380
- value: function renderLoginFormWithPromise() {
381
- var _this6 = this;
382
-
383
- return new Promise(function (resolve, reject) {
384
- _this6.buildUpLoginFormHtml();
385
-
386
- _this6.bindLoginFormSubmit().then(function () {
238
+ window.addEventListener('message', waitForPostMessage, false);
239
+ timeout = setTimeout(() => {
240
+ if (this.setCookieLoading === true) {
241
+ //console.log('timeout');
242
+ window.removeEventListener('message', waitForPostMessage, false);
243
+ _helpers.default.remove(document.querySelector('.iframe_wrapper'));
244
+ this.setCookieLoading = false;
387
245
  resolve();
388
- }).catch(function (error) {
389
- reject(error);
246
+ }
247
+ }, 20000);
248
+ this.config.sso.forEach(sso__value => {
249
+ if (sso__value === window.location.protocol + '//' + window.location.host) {
250
+ return;
251
+ }
252
+ let iframe = document.createElement('iframe');
253
+ iframe.setAttribute('src', sso__value + '/sso.html');
254
+ iframe.style.width = '1px';
255
+ iframe.style.height = '1px';
256
+ iframe.addEventListener('load', e => {
257
+ iframe.contentWindow.postMessage({
258
+ access_token: access_token
259
+ }, sso__value);
390
260
  });
391
-
392
- _this6.triggerLoginFormRenderedEvent();
261
+ document.querySelector('.iframe_wrapper').appendChild(iframe);
262
+ });
263
+ });
264
+ }
265
+ renderLoginFormWithPromise() {
266
+ return new Promise((resolve, reject) => {
267
+ this.buildUpLoginFormHtml();
268
+ this.bindLoginFormSubmit().then(() => {
269
+ resolve();
270
+ }).catch(error => {
271
+ reject(error);
393
272
  });
273
+ this.triggerLoginFormRenderedEvent();
274
+ });
275
+ }
276
+ buildUpLoginFormHtml() {
277
+ if (!('login_form' in this.config) || this.config.login_form == '') {
278
+ this.config.login_form = "<div class=\"login-form\">\n <div class=\"login-form__inner\">\n <form class=\"login-form__form\">\n <ul class=\"login-form__items\">\n <li class=\"login-form__item\">\n <label class=\"login-form__label login-form__label--".concat(this.config.auth_login, "\" for=\"login-form__label--").concat(this.config.auth_login, "\">").concat(this.config.auth_login === 'email' ? 'E-Mail-Adresse' : this.config.auth_login === 'username' ? 'Benutzername' : this.config.auth_login, "</label>\n <input class=\"login-form__input login-form__input--").concat(this.config.auth_login, "\" id=\"login-form__label--").concat(this.config.auth_login, "\" type=\"text\" required=\"required\" name=\"").concat(this.config.auth_login, "\" />\n </li>\n <li class=\"login-form__item\">\n <label class=\"login-form__label login-form__label--password\" for=\"login-form__label--password\">Passwort</label>\n <input class=\"login-form__input login-form__input--password\" id=\"login-form__label--password\" type=\"password\" required=\"required\" name=\"password\" />\n </li>\n <li class=\"login-form__item\">\n <input class=\"login-form__submit\" type=\"submit\" value=\"Anmelden\" />\n </li>\n </ul>\n </form>\n </div>\n </div>");
394
279
  }
395
- }, {
396
- key: "buildUpLoginFormHtml",
397
- value: function buildUpLoginFormHtml() {
398
- if (!('login_form' in this.config) || this.config.login_form == '') {
399
- this.config.login_form = "<div class=\"login-form\">\n <div class=\"login-form__inner\">\n <form class=\"login-form__form\">\n <ul class=\"login-form__items\">\n <li class=\"login-form__item\">\n <label class=\"login-form__label login-form__label--".concat(this.config.auth_login, "\" for=\"login-form__label--").concat(this.config.auth_login, "\">").concat(this.config.auth_login === 'email' ? 'E-Mail-Adresse' : this.config.auth_login === 'username' ? 'Benutzername' : this.config.auth_login, "</label>\n <input class=\"login-form__input login-form__input--").concat(this.config.auth_login, "\" id=\"login-form__label--").concat(this.config.auth_login, "\" type=\"text\" required=\"required\" name=\"").concat(this.config.auth_login, "\" />\n </li>\n <li class=\"login-form__item\">\n <label class=\"login-form__label login-form__label--password\" for=\"login-form__label--password\">Passwort</label>\n <input class=\"login-form__input login-form__input--password\" id=\"login-form__label--password\" type=\"password\" required=\"required\" name=\"password\" />\n </li>\n <li class=\"login-form__item\">\n <input class=\"login-form__submit\" type=\"submit\" value=\"Anmelden\" />\n </li>\n </ul>\n </form>\n </div>\n </div>");
400
- }
401
-
402
- var dom = new DOMParser().parseFromString(this.config.login_form, 'text/html').body.childNodes[0];
403
- this.config.login_form_class = dom.getAttribute('class').split(' ')[0];
404
-
405
- _helpers.default.remove(document.querySelector('.' + this.config.login_form_class));
406
-
407
- this.addLoadingState('login-form-visible');
408
- var parent = document.body;
409
-
410
- if ('login_form_parent' in this.config && this.config.login_form_parent != '' && document.querySelector(this.config.login_form_parent) !== null) {
411
- parent = document.querySelector(this.config.login_form_parent);
412
- }
413
-
414
- parent.appendChild(dom);
280
+ let dom = new DOMParser().parseFromString(this.config.login_form, 'text/html').body.childNodes[0];
281
+ this.config.login_form_class = dom.getAttribute('class').split(' ')[0];
282
+ _helpers.default.remove(document.querySelector('.' + this.config.login_form_class));
283
+ this.addLoadingState('login-form-visible');
284
+ let parent = document.body;
285
+ if ('login_form_parent' in this.config && this.config.login_form_parent != '' && document.querySelector(this.config.login_form_parent) !== null) {
286
+ parent = document.querySelector(this.config.login_form_parent);
415
287
  }
416
- }, {
417
- key: "triggerLoginFormRenderedEvent",
418
- value: function triggerLoginFormRenderedEvent() {
419
- if ('login_form_rendered' in this.config && this.config.login_form_rendered != '' && typeof this.config.login_form_rendered === 'function') {
420
- this.config.login_form_rendered(document.querySelector('.' + this.config.login_form_class));
421
- }
288
+ parent.appendChild(dom);
289
+ }
290
+ triggerLoginFormRenderedEvent() {
291
+ if ('login_form_rendered' in this.config && this.config.login_form_rendered != '' && typeof this.config.login_form_rendered === 'function') {
292
+ this.config.login_form_rendered(document.querySelector('.' + this.config.login_form_class));
422
293
  }
423
- }, {
424
- key: "bindLoginFormSubmit",
425
- value: function bindLoginFormSubmit() {
426
- var _this7 = this;
427
-
428
- return new Promise(function (resolve, reject) {
429
- var dom = document.querySelector('.' + _this7.config.login_form_class),
430
- form = dom.querySelector('form');
431
- form.addEventListener('submit', function (e) {
432
- var _JSON$stringify;
433
-
434
- _this7.addLoadingState('logging-in');
435
-
294
+ }
295
+ bindLoginFormSubmit() {
296
+ return new Promise((resolve, reject) => {
297
+ let dom = document.querySelector('.' + this.config.login_form_class),
298
+ form = dom.querySelector('form');
299
+ form.addEventListener('submit', e => {
300
+ this.addLoadingState('logging-in');
301
+ if (form.querySelector('input[type="submit"]') !== null) {
302
+ form.querySelector('input[type="submit"]').disabled = true;
303
+ }
304
+ _helpers.default.remove(dom.querySelector('.' + this.config.login_form_class + '__error'));
305
+ fetch(this.config.auth_server + '/login', {
306
+ method: 'POST',
307
+ body: JSON.stringify({
308
+ [this.config.auth_login]: form.querySelector('input[name="' + this.config.auth_login + '"]').value,
309
+ password: form.querySelector('input[name="password"]').value
310
+ }),
311
+ headers: {
312
+ 'content-type': 'application/json'
313
+ },
314
+ cache: 'no-cache'
315
+ }).then(res => res.json()).catch(err => err).then(response => {
436
316
  if (form.querySelector('input[type="submit"]') !== null) {
437
- form.querySelector('input[type="submit"]').disabled = true;
317
+ form.querySelector('input[type="submit"]').disabled = false;
438
318
  }
439
-
440
- _helpers.default.remove(dom.querySelector('.' + _this7.config.login_form_class + '__error'));
441
-
442
- fetch(_this7.config.auth_server + '/login', {
443
- method: 'POST',
444
- body: JSON.stringify((_JSON$stringify = {}, (0, _defineProperty2.default)(_JSON$stringify, _this7.config.auth_login, form.querySelector('input[name="' + _this7.config.auth_login + '"]').value), (0, _defineProperty2.default)(_JSON$stringify, "password", form.querySelector('input[name="password"]').value), _JSON$stringify)),
445
- headers: {
446
- 'content-type': 'application/json'
447
- },
448
- cache: 'no-cache'
449
- }).then(function (res) {
450
- return res.json();
451
- }).catch(function (err) {
452
- return err;
453
- }).then(function (response) {
454
- if (form.querySelector('input[type="submit"]') !== null) {
455
- form.querySelector('input[type="submit"]').disabled = false;
456
- }
457
-
458
- if (response !== undefined && response !== null && 'success' in response && response.success === true) {
459
- _helpers.default.remove(document.querySelector('.' + _this7.config.login_form_class));
460
-
461
- _this7.setCookies(response.data.access_token).then(function () {
462
- _this7.removeLoadingStates();
463
-
464
- resolve();
465
- }).catch(function (error) {
466
- reject(error);
467
- });
468
- } else {
469
- form.insertAdjacentHTML('afterbegin', '<div class="' + _this7.config.login_form_class + '__error">' + response.public_message + '</div>');
470
- }
471
- });
472
- e.preventDefault();
473
- }, false);
474
- });
475
- }
476
- }, {
477
- key: "addLoadingState",
478
- value: function addLoadingState(state) {
479
- document.documentElement.classList.add('jwtbutler-' + state);
480
-
481
- if (state === 'logging-in' || state === 'logging-out') {
482
- document.documentElement.classList.add('jwtbutler-loading');
483
- }
484
- }
485
- }, {
486
- key: "removeLoadingStates",
487
- value: function removeLoadingStates() {
488
- document.documentElement.classList.remove('jwtbutler-logging-in');
489
- document.documentElement.classList.remove('jwtbutler-logging-out');
490
- document.documentElement.classList.remove('jwtbutler-loading');
491
- document.documentElement.classList.remove('jwtbutler-fetching');
492
- document.documentElement.classList.remove('jwtbutler-login-form-visible');
319
+ if (response !== undefined && response !== null && 'success' in response && response.success === true) {
320
+ _helpers.default.remove(document.querySelector('.' + this.config.login_form_class));
321
+ this.setCookies(response.data.access_token).then(() => {
322
+ this.removeLoadingStates();
323
+ resolve();
324
+ }).catch(error => {
325
+ reject(error);
326
+ });
327
+ } else {
328
+ form.insertAdjacentHTML('afterbegin', '<div class="' + this.config.login_form_class + '__error">' + response.public_message + '</div>');
329
+ }
330
+ });
331
+ e.preventDefault();
332
+ }, false);
333
+ });
334
+ }
335
+ addLoadingState(state) {
336
+ document.documentElement.classList.add('jwtbutler-' + state);
337
+ if (state === 'logging-in' || state === 'logging-out') {
338
+ document.documentElement.classList.add('jwtbutler-loading');
493
339
  }
494
- }]);
495
- return jwtbutler;
496
- }();
497
-
340
+ }
341
+ removeLoadingStates() {
342
+ document.documentElement.classList.remove('jwtbutler-logging-in');
343
+ document.documentElement.classList.remove('jwtbutler-logging-out');
344
+ document.documentElement.classList.remove('jwtbutler-loading');
345
+ document.documentElement.classList.remove('jwtbutler-fetching');
346
+ document.documentElement.classList.remove('jwtbutler-login-form-visible');
347
+ }
348
+ }
498
349
  exports.default = jwtbutler;
499
350
  window.jwtbutler = jwtbutler;