@squiz/resource-browser 2.1.9-rc.0 → 2.2.0-rc.0
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.
- package/.storybook/preview-body.html +1 -0
- package/.storybook/preview-head.html +7 -10
- package/CHANGELOG.md +10 -0
- package/lib/Hooks/useAuth.d.ts +7 -0
- package/lib/Hooks/useAuth.js +56 -0
- package/lib/MainContainer/MainContainer.js +1 -1
- package/lib/Plugin/Plugin.js +3 -1
- package/lib/ResourceBrowserContext/AuthProvider.d.ts +16 -0
- package/lib/ResourceBrowserContext/AuthProvider.js +46 -0
- package/lib/index.css +326 -326
- package/lib/index.d.ts +2 -1
- package/lib/index.js +5 -1
- package/lib/types.d.ts +11 -2
- package/lib/utils/authUtils.d.ts +5 -0
- package/lib/utils/authUtils.js +38 -0
- package/package.json +3 -3
- package/postcss.config.js +19 -15
- package/src/Hooks/useAuth.spec.tsx +137 -0
- package/src/Hooks/useAuth.ts +60 -0
- package/src/MainContainer/MainContainer.tsx +1 -1
- package/src/Plugin/Plugin.tsx +2 -1
- package/src/ResourceBrowserContext/AuthProvider.spec.tsx +73 -0
- package/src/ResourceBrowserContext/AuthProvider.tsx +40 -0
- package/src/index.spec.tsx +3 -1
- package/src/index.tsx +3 -1
- package/src/types.ts +13 -2
- package/src/utils/authUtils.spec.ts +88 -0
- package/src/utils/authUtils.ts +40 -0
@@ -0,0 +1,40 @@
|
|
1
|
+
import { AuthenticationConfiguration } from '../types';
|
2
|
+
|
3
|
+
export const getCookieValue = (name: string): string | null => {
|
4
|
+
const match = document.cookie.match('(^|;)\\s*' + name + '\\s*=\\s*([^;]+)');
|
5
|
+
return match ? match.pop()! : null;
|
6
|
+
};
|
7
|
+
|
8
|
+
export const setCookieValue = (name: string, value: string): void => {
|
9
|
+
document.cookie = `${name}=${value}; Path=/;`;
|
10
|
+
};
|
11
|
+
|
12
|
+
export const logout = (): void => {
|
13
|
+
setCookieValue('authToken', '');
|
14
|
+
setCookieValue('refreshToken', '');
|
15
|
+
};
|
16
|
+
|
17
|
+
export const refreshAccessToken = async (authConfig?: AuthenticationConfiguration): Promise<string> => {
|
18
|
+
if (!authConfig) {
|
19
|
+
throw new Error('No auth configuration available');
|
20
|
+
}
|
21
|
+
|
22
|
+
const refreshToken = getCookieValue('refreshToken');
|
23
|
+
if (!refreshToken) {
|
24
|
+
throw new Error('You are not logged in');
|
25
|
+
}
|
26
|
+
|
27
|
+
const response = await fetch(`${authConfig.redirectUrl}?grant_type=refresh_token&refresh_token=${refreshToken}`, {
|
28
|
+
method: 'GET',
|
29
|
+
credentials: 'include',
|
30
|
+
});
|
31
|
+
|
32
|
+
if (!response.ok) {
|
33
|
+
logout();
|
34
|
+
throw new Error('Failed to refresh token');
|
35
|
+
}
|
36
|
+
|
37
|
+
const data = await response.json();
|
38
|
+
setCookieValue('authToken', data.access_token);
|
39
|
+
return data.access_token;
|
40
|
+
};
|