@omerlo/omerlo-webkit 0.0.32 → 0.0.34

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.
@@ -19,5 +19,6 @@ export interface OauthUser {
19
19
  id: string;
20
20
  name: string;
21
21
  email: string;
22
+ accessToken: string;
22
23
  verifiedAt: Date | null;
23
24
  }
@@ -33,6 +33,7 @@ export function parseOauthUser(data, _assoc) {
33
33
  id: data.id,
34
34
  name: data.name,
35
35
  email: data.email,
36
+ accessToken: data.access_token,
36
37
  verifiedAt: data.verified_at
37
38
  };
38
39
  }
@@ -52,7 +52,8 @@ const applicationToken = {
52
52
  accessToken: '',
53
53
  refreshToken: '',
54
54
  expiredAt: 0,
55
- init: false
55
+ init: false,
56
+ refreshErrorCounter: 0
56
57
  };
57
58
  /**
58
59
  * Get the token used by the application.
@@ -66,16 +67,36 @@ export async function getApplicationToken() {
66
67
  }
67
68
  return applicationToken.accessToken;
68
69
  }
70
+ let refreshingPromise = null;
69
71
  async function refreshApplicationToken() {
70
72
  if (!applicationToken.refreshToken) {
71
73
  throw new Error('Could not refresh the application token because the refresh token is null');
72
74
  }
73
- const token = await refresh(applicationToken.refreshToken);
74
- applicationToken.accessToken = token.accessToken;
75
- applicationToken.refreshToken = token.refreshToken;
76
- const date = new Date();
77
- const timestamps = date.setSeconds(date.getSeconds() + token.expiresIn - 60);
78
- applicationToken.expiredAt = timestamps;
75
+ if (refreshingPromise) {
76
+ return refreshingPromise;
77
+ }
78
+ refreshingPromise = (async () => {
79
+ try {
80
+ const token = await refresh(applicationToken.refreshToken);
81
+ applicationToken.accessToken = token.accessToken;
82
+ applicationToken.refreshToken = token.refreshToken;
83
+ const date = new Date();
84
+ const timestamps = date.setSeconds(date.getSeconds() + token.expiresIn - 60);
85
+ applicationToken.expiredAt = timestamps;
86
+ applicationToken.refreshErrorCounter = 0;
87
+ }
88
+ catch (e) {
89
+ applicationToken.refreshErrorCounter += 1;
90
+ if (applicationToken.refreshErrorCounter >= 10) {
91
+ applicationToken.init = false;
92
+ }
93
+ throw e;
94
+ }
95
+ finally {
96
+ refreshingPromise = null;
97
+ }
98
+ })();
99
+ return refreshingPromise;
79
100
  }
80
101
  async function newApplicationToken() {
81
102
  const token = await getAnonymousToken('application');
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@omerlo/omerlo-webkit",
3
- "version": "0.0.32",
3
+ "version": "0.0.34",
4
4
  "scripts": {
5
5
  "dev": "vite dev",
6
6
  "build": "vite build && npm run package",