flamerest 1.0.68 → 1.0.70

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.
Files changed (3) hide show
  1. package/REST.d.ts +1 -0
  2. package/REST.js +24 -4
  3. package/package.json +1 -1
package/REST.d.ts CHANGED
@@ -129,6 +129,7 @@ type Authorized = {
129
129
  role: string
130
130
  };
131
131
  errors: { [key: string]: any };
132
+ token: string;
132
133
  }
133
134
 
134
135
  /**
package/REST.js CHANGED
@@ -30,6 +30,12 @@ class FLAMEREST {
30
30
  */
31
31
  this.version = version ? version : 'v1';
32
32
 
33
+ /**
34
+ * Если авторизация по токену, то он сюда подставляется
35
+ */
36
+ this.isAuthByJWTQuery = true;
37
+ this.token = null;
38
+
33
39
  }
34
40
 
35
41
  install(Vue, options) {
@@ -44,7 +50,7 @@ class FLAMEREST {
44
50
  * @param {string} type Тип
45
51
  * @param {string} responseType Тип ответа: json или blob
46
52
  */
47
- request(url, params, type = 'GET', responseType = 'json') {
53
+ request(url, params, type = 'GET', responseType = 'json', isNeedToken = true) {
48
54
 
49
55
  // Нормализуем параметры, если они есть
50
56
  if (typeof params === "object") {
@@ -60,6 +66,11 @@ class FLAMEREST {
60
66
 
61
67
  try {
62
68
 
69
+ // Авторизация
70
+ if (this.isAuthByJWTQuery && isNeedToken === true && this.token !== null) {
71
+ url = url + "?access-token=" + this.token;
72
+ }
73
+
63
74
  // Тело запроса
64
75
  let requestBody = {
65
76
  method: type,
@@ -468,20 +479,29 @@ class FLAMEREST {
468
479
  return new Promise((resolve, reject) => { resolve(JSON.parse(window.sessionStorage.getItem("crudschema"))) });
469
480
  }
470
481
 
471
- async auth(username, password) {
482
+ async auth(username, password, isNeedToken) {
472
483
 
473
- let resp = await this.request(this.SERVER + '/auth/auth', JSON.stringify({ login: username, password: password }), 'POST');
484
+ let resp = null;
485
+ if (this.token !== null && this.token !== 'undefined' && (username === null || username === undefined)) {
486
+ resp = await this.request(this.SERVER + '/auth/auth', JSON.stringify({}), 'POST', 'json', true);
487
+ }
488
+ else {
489
+ resp = await this.request(this.SERVER + '/auth/auth', JSON.stringify({ login: username, password: password }), 'POST', 'json', false);
490
+ }
474
491
 
475
492
  if (resp.errors) return resp;
476
493
  if (resp.data.length === 0) { resp.errors = []; return resp; }
477
494
 
495
+ // после успешной авторизации устанавливаем токен
496
+ if (typeof resp.token === 'string') this.token = resp.token;
497
+
478
498
  return resp.data;
479
499
 
480
500
  }
481
501
 
482
502
  async signup(email, username, password, name) {
483
503
 
484
- let resp = await this.request(this.SERVER + '/auth/signup', JSON.stringify({ login: username, email: email, password: password, name: name }), 'POST');
504
+ let resp = await this.request(this.SERVER + '/auth/signup', JSON.stringify({ login: username, email: email, password: password, name: name }), 'POST', 'json', false);
485
505
 
486
506
  if (resp.errors) return resp;
487
507
  if (resp.data.length === 0) { resp.errors = []; return resp; }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flamerest",
3
- "version": "1.0.68",
3
+ "version": "1.0.70",
4
4
  "description": "",
5
5
  "main": "REST.js",
6
6
  "typings": "./REST.d.ts",