@go-avro/avro-js 0.0.2-beta.4 → 0.0.2-beta.6
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.
|
@@ -6,7 +6,7 @@ export declare class AuthManager {
|
|
|
6
6
|
baseUrl: string;
|
|
7
7
|
storage: TokenStorage | TokenStorage[];
|
|
8
8
|
});
|
|
9
|
-
|
|
9
|
+
isAuthenticated(): Promise<boolean>;
|
|
10
10
|
fetchNewTokens(): Promise<Tokens>;
|
|
11
11
|
accessToken(): Promise<string | undefined>;
|
|
12
12
|
refreshTokens(): Promise<Tokens | null>;
|
package/dist/auth/AuthManager.js
CHANGED
|
@@ -11,11 +11,42 @@ export class AuthManager {
|
|
|
11
11
|
});
|
|
12
12
|
this.baseUrl = baseUrl;
|
|
13
13
|
}
|
|
14
|
-
async
|
|
14
|
+
async isAuthenticated() {
|
|
15
|
+
if (!this.storages.length) {
|
|
16
|
+
throw new Error('No token storages initialized');
|
|
17
|
+
}
|
|
15
18
|
for (const storage of this.storages) {
|
|
16
19
|
const tokens = await storage.get();
|
|
17
20
|
if (tokens && tokens.access_token) {
|
|
18
|
-
|
|
21
|
+
try {
|
|
22
|
+
const response = await fetch(`${this.baseUrl}/validate`, {
|
|
23
|
+
method: 'POST',
|
|
24
|
+
headers: {
|
|
25
|
+
'Content-Type': 'application/json',
|
|
26
|
+
'Authorization': `Bearer ${tokens.access_token}`,
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
if (response.ok) {
|
|
30
|
+
return true;
|
|
31
|
+
}
|
|
32
|
+
else {
|
|
33
|
+
// Attempt token refresh if validation fails
|
|
34
|
+
const newTokens = await this.refreshTokens();
|
|
35
|
+
if (newTokens && newTokens.access_token) {
|
|
36
|
+
const retryResponse = await fetch(`${this.baseUrl}/validate`, {
|
|
37
|
+
method: 'POST',
|
|
38
|
+
headers: {
|
|
39
|
+
'Content-Type': 'application/json',
|
|
40
|
+
'Authorization': `Bearer ${newTokens.access_token}`,
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
return retryResponse.ok;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
catch (error) {
|
|
48
|
+
console.error('Error validating access token:', error);
|
|
49
|
+
}
|
|
19
50
|
}
|
|
20
51
|
}
|
|
21
52
|
return false;
|