@omerlo/omerlo-webkit 0.0.32 → 0.0.33
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.
|
@@ -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
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
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');
|