curtain-web-api 1.0.0 → 1.0.2

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.
@@ -14,7 +14,7 @@ export declare class CurtainWebAPI {
14
14
  constructor(baseURL?: string);
15
15
  login(username: string, password: string): Promise<User>;
16
16
  getUserInfo(): Promise<User>;
17
- logout(): Promise<import("axios").AxiosResponse<any, any>>;
17
+ logout(): Promise<void>;
18
18
  refresh(): Promise<import("axios").AxiosResponse<any, any>>;
19
19
  ORCIDLogin(authorizationCode: string, redirectURI: string): Promise<User>;
20
20
  checkIfRefreshTokenExpired(): boolean;
@@ -67,7 +67,12 @@ class CurtainWebAPI {
67
67
  this.userInfoURL = baseURL + "user/";
68
68
  this.axiosInstance.interceptors.request.use((config) => {
69
69
  if (config.url) {
70
- if (config.url === this.logoutURL || config.url === this.userInfoURL || config.url.startsWith(this.baseURL + "curtain/") || config.url.startsWith(this.baseURL + "data_filter_list/")) {
70
+ if (
71
+ //config.url === this.refereshURL ||
72
+ config.url === this.logoutURL ||
73
+ config.url === this.userInfoURL ||
74
+ config.url.startsWith(this.baseURL + "curtain/") ||
75
+ config.url.startsWith(this.baseURL + "data_filter_list/")) {
71
76
  if (this.user.loginStatus) {
72
77
  config.headers["Authorization"] = "Bearer " + this.user.access_token;
73
78
  }
@@ -87,6 +92,7 @@ class CurtainWebAPI {
87
92
  if (!this.checkIfRefreshTokenExpired() && this.user.loginStatus) {
88
93
  if (!this.isRefreshing) {
89
94
  return this.refresh().then((response) => {
95
+ this.isRefreshing = false;
90
96
  return this.axiosInstance.request(error.config);
91
97
  }).catch((error) => {
92
98
  this.isRefreshing = false;
@@ -134,9 +140,7 @@ class CurtainWebAPI {
134
140
  headers["Accept"] = "application/json";
135
141
  headers["Content-Type"] = "application/json";
136
142
  return this.axiosInstance.post(this.logoutURL, { refresh_token: this.user.refresh_token }, { headers: headers, responseType: "json" }).then((response) => {
137
- this.user.clearDB();
138
- this.user = new curtain_user_1.User();
139
- return response;
143
+ return this.user.clearDB();
140
144
  });
141
145
  }
142
146
  refresh() {
@@ -167,7 +171,7 @@ class CurtainWebAPI {
167
171
  let now = new Date();
168
172
  let diff = (now.getTime() - this.user.lastRefreshTokenUpdate.getTime()) / 1000;
169
173
  diff = diff / 60 / 60;
170
- return 24 > Math.abs(Math.round(diff));
174
+ return 24 <= Math.abs(Math.round(diff));
171
175
  }
172
176
  deleteCurtainLink(curtainLinkID) {
173
177
  let headers = new axios_1.AxiosHeaders();
@@ -27,4 +27,5 @@ export declare class User {
27
27
  saveIntoDB(): Promise<void>;
28
28
  loadFromDB(): Promise<void>;
29
29
  clearDB(): Promise<void>;
30
+ reset(): void;
30
31
  }
@@ -127,11 +127,26 @@ class User {
127
127
  }
128
128
  clearDB() {
129
129
  return this.db.destroy().then((response) => {
130
+ this.reset();
130
131
  this.db = new pouchdb_1.default("curtainuser");
131
132
  return this.initiateDB();
132
133
  }).catch((error) => {
133
134
  console.log(error);
134
135
  });
135
136
  }
137
+ reset() {
138
+ this.access_token = "";
139
+ this.refresh_token = "";
140
+ this.username = "";
141
+ this.loginStatus = false;
142
+ this.isStaff = false;
143
+ this.id = 0;
144
+ this.totalCurtain = 0;
145
+ this.canDelete = false;
146
+ this.curtainLinkLimit = 0;
147
+ this.curtainLinkLimitExceeded = false;
148
+ this.lastAccessTokenUpdate = new Date();
149
+ this.lastRefreshTokenUpdate = new Date();
150
+ }
136
151
  }
137
152
  exports.User = User;
Binary file
package/curtainuser/LOG CHANGED
@@ -1 +1 @@
1
- 2023/04/15-16:03:03.770 2568 Delete type=3 #1
1
+ 2023/04/15-17:56:07.976 99c Delete type=3 #1
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "curtain-web-api",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "description": "",
5
5
  "main": "./build/index.js",
6
6
  "types": "./build/index.d.ts",
@@ -43,7 +43,12 @@ export class CurtainWebAPI {
43
43
  this.userInfoURL = baseURL + "user/";
44
44
  this.axiosInstance.interceptors.request.use((config) => {
45
45
  if (config.url) {
46
- if (config.url === this.logoutURL || config.url === this.userInfoURL || config.url.startsWith(this.baseURL + "curtain/") || config.url.startsWith(this.baseURL + "data_filter_list/")) {
46
+ if (
47
+ //config.url === this.refereshURL ||
48
+ config.url === this.logoutURL ||
49
+ config.url === this.userInfoURL ||
50
+ config.url.startsWith(this.baseURL + "curtain/") ||
51
+ config.url.startsWith(this.baseURL + "data_filter_list/")) {
47
52
  if (this.user.loginStatus) {
48
53
  config.headers["Authorization"] = "Bearer " + this.user.access_token;
49
54
  }
@@ -64,6 +69,7 @@ export class CurtainWebAPI {
64
69
  if (!this.checkIfRefreshTokenExpired() && this.user.loginStatus) {
65
70
  if (!this.isRefreshing) {
66
71
  return this.refresh().then((response) => {
72
+ this.isRefreshing = false;
67
73
  return this.axiosInstance.request(error.config);
68
74
  }).catch((error) => {
69
75
  this.isRefreshing = false;
@@ -116,9 +122,7 @@ export class CurtainWebAPI {
116
122
  headers["Accept"] = "application/json";
117
123
  headers["Content-Type"] = "application/json";
118
124
  return this.axiosInstance.post(this.logoutURL, {refresh_token: this.user.refresh_token}, {headers: headers, responseType:"json"}).then((response) => {
119
- this.user.clearDB();
120
- this.user = new User();
121
- return response;
125
+ return this.user.clearDB();
122
126
  });
123
127
  }
124
128
 
@@ -152,7 +156,7 @@ export class CurtainWebAPI {
152
156
  let now = new Date();
153
157
  let diff = (now.getTime() - this.user.lastRefreshTokenUpdate.getTime()) / 1000;
154
158
  diff = diff/60/60;
155
- return 24 > Math.abs(Math.round(diff));
159
+ return 24 <= Math.abs(Math.round(diff));
156
160
  }
157
161
 
158
162
  deleteCurtainLink(curtainLinkID: string) {
@@ -137,10 +137,26 @@ export class User {
137
137
 
138
138
  clearDB() {
139
139
  return this.db.destroy().then((response) => {
140
+ this.reset()
140
141
  this.db = new PouchDB("curtainuser");
141
142
  return this.initiateDB();
142
143
  }).catch((error) => {
143
144
  console.log(error);
144
145
  });
145
146
  }
147
+
148
+ reset() {
149
+ this.access_token = "";
150
+ this.refresh_token = "";
151
+ this.username = "";
152
+ this.loginStatus = false;
153
+ this.isStaff = false;
154
+ this.id = 0;
155
+ this.totalCurtain = 0;
156
+ this.canDelete = false;
157
+ this.curtainLinkLimit = 0;
158
+ this.curtainLinkLimitExceeded = false;
159
+ this.lastAccessTokenUpdate = new Date();
160
+ this.lastRefreshTokenUpdate = new Date();
161
+ }
146
162
  }
@@ -34,6 +34,19 @@ describe('User', function () {
34
34
  await user.clearDB();
35
35
  await Promise.resolve()
36
36
 
37
+ assert.ok(true)
38
+ });
39
+ it("destroying user data from local storage", async () => {
40
+ const user = new User();
41
+ await user.init();
42
+ for (const i in userEx) {
43
+ user[i] = userEx[i]
44
+ }
45
+ await user.updateDB();
46
+ await user.clearDB();
47
+ await user.loadFromDB();
48
+ expect(user.username).to.equal("");
49
+ await Promise.resolve()
37
50
  assert.ok(true)
38
51
  })
39
52
  })
@@ -66,6 +79,7 @@ describe("Login", () => {
66
79
  it("check token expiry", async () => {
67
80
  const curtainLogin = new CurtainWebAPI();
68
81
  await curtainLogin.user.loadFromDB()
82
+ console.log(curtainLogin.user.lastRefreshTokenUpdate)
69
83
  const expired = curtainLogin.checkIfRefreshTokenExpired()
70
84
  expect(expired).to.equal(false)
71
85
  await Promise.resolve()
@@ -75,10 +89,22 @@ describe("Login", () => {
75
89
  })
76
90
 
77
91
  describe("Session data", () => {
92
+ it('should retrieve session meta data', async () => {
93
+ const curtainLogin = new CurtainWebAPI();
94
+ const result = await curtainLogin.getSessionSettings("546c9ed7-30a6-4a0f-aedb-880815eb7051")
95
+ });
96
+
78
97
  it('should retrieve session data', async () => {
79
98
  const curtainLogin = new CurtainWebAPI();
80
99
  const result = await curtainLogin.postSettings("546c9ed7-30a6-4a0f-aedb-880815eb7051", "")
81
- console.log(result)
100
+ })
101
+ it('should login and then retrieve session data', async () => {
102
+ const curtainLogin = new CurtainWebAPI();
103
+ await curtainLogin.login("testroot", "testpassword")
104
+ const previousAccess = curtainLogin.user.access_token.slice()
105
+ await curtainLogin.refresh()
106
+ expect(previousAccess).to.not.equal(curtainLogin.user.access_token)
107
+ const result = await curtainLogin.postSettings("546c9ed7-30a6-4a0f-aedb-880815eb7051", "")
82
108
  })
83
109
  });
84
110