@backstage/plugin-auth-react 0.0.0-nightly-20240409021110 → 0.0.0-nightly-20240412021259
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/CHANGELOG.md +23 -2
- package/dist/index.d.ts +8 -4
- package/dist/index.esm.js +36 -6
- package/dist/index.esm.js.map +1 -1
- package/package.json +6 -5
package/CHANGELOG.md
CHANGED
|
@@ -1,11 +1,32 @@
|
|
|
1
1
|
# @backstage/plugin-auth-react
|
|
2
2
|
|
|
3
|
-
## 0.0.0-nightly-
|
|
3
|
+
## 0.0.0-nightly-20240412021259
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- c884b9a: **BREAKING**: Removed the path option from `CookieAuthRefreshProvider` and `useCookieAuthRefresh`.
|
|
8
|
+
|
|
9
|
+
A new `CookieAuthRedirect` component has been added to redirect a public app bundle to the protected one when using the `app-backend` with a separate public entry point.
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- Updated dependencies
|
|
14
|
+
- @backstage/core-components@0.0.0-nightly-20240412021259
|
|
15
|
+
- @backstage/core-plugin-api@1.9.1
|
|
16
|
+
- @backstage/errors@1.2.4
|
|
17
|
+
|
|
18
|
+
## 0.1.0-next.1
|
|
19
|
+
|
|
20
|
+
### Minor Changes
|
|
21
|
+
|
|
22
|
+
- c884b9a: **BREAKING**: Removed the path option from `CookieAuthRefreshProvider` and `useCookieAuthRefresh`.
|
|
23
|
+
|
|
24
|
+
A new `CookieAuthRedirect` component has been added to redirect a public app bundle to the protected one when using the `app-backend` with a separate public entry point.
|
|
4
25
|
|
|
5
26
|
### Patch Changes
|
|
6
27
|
|
|
7
28
|
- Updated dependencies
|
|
8
|
-
- @backstage/core-components@0.
|
|
29
|
+
- @backstage/core-components@0.14.4-next.0
|
|
9
30
|
- @backstage/core-plugin-api@1.9.1
|
|
10
31
|
- @backstage/errors@1.2.4
|
|
11
32
|
|
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,10 @@
|
|
|
1
|
-
import { ReactNode } from 'react';
|
|
1
|
+
import React, { ReactNode } from 'react';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* @public
|
|
5
|
+
* A component that redirects to the page an user was trying to access before sign-in.
|
|
6
|
+
*/
|
|
7
|
+
declare function CookieAuthRedirect(): React.JSX.Element | null;
|
|
2
8
|
|
|
3
9
|
/**
|
|
4
10
|
* @public
|
|
@@ -6,7 +12,6 @@ import { ReactNode } from 'react';
|
|
|
6
12
|
*/
|
|
7
13
|
type CookieAuthRefreshProviderProps = {
|
|
8
14
|
pluginId: string;
|
|
9
|
-
path?: string;
|
|
10
15
|
children: ReactNode;
|
|
11
16
|
};
|
|
12
17
|
/**
|
|
@@ -22,7 +27,6 @@ declare function CookieAuthRefreshProvider(props: CookieAuthRefreshProviderProps
|
|
|
22
27
|
*/
|
|
23
28
|
declare function useCookieAuthRefresh(options: {
|
|
24
29
|
pluginId: string;
|
|
25
|
-
path?: string;
|
|
26
30
|
}): {
|
|
27
31
|
status: 'loading';
|
|
28
32
|
} | {
|
|
@@ -36,4 +40,4 @@ declare function useCookieAuthRefresh(options: {
|
|
|
36
40
|
};
|
|
37
41
|
};
|
|
38
42
|
|
|
39
|
-
export { CookieAuthRefreshProvider, type CookieAuthRefreshProviderProps, useCookieAuthRefresh };
|
|
43
|
+
export { CookieAuthRedirect, CookieAuthRefreshProvider, type CookieAuthRefreshProviderProps, useCookieAuthRefresh };
|
package/dist/index.esm.js
CHANGED
|
@@ -1,12 +1,42 @@
|
|
|
1
1
|
import React, { useMemo, useCallback, useEffect } from 'react';
|
|
2
|
-
import {
|
|
3
|
-
import { useApi, fetchApiRef, discoveryApiRef, useApp } from '@backstage/core-plugin-api';
|
|
4
|
-
import { Button } from '@material-ui/core';
|
|
2
|
+
import { useApi, identityApiRef, fetchApiRef, discoveryApiRef, useApp } from '@backstage/core-plugin-api';
|
|
5
3
|
import { useAsync, useMountEffect } from '@react-hookz/web';
|
|
4
|
+
import { ErrorPanel } from '@backstage/core-components';
|
|
5
|
+
import Button from '@material-ui/core/Button';
|
|
6
6
|
import { ResponseError } from '@backstage/errors';
|
|
7
7
|
|
|
8
|
+
function CookieAuthRedirect() {
|
|
9
|
+
const identityApi = useApi(identityApiRef);
|
|
10
|
+
const [state, actions] = useAsync(async () => {
|
|
11
|
+
const { token } = await identityApi.getCredentials();
|
|
12
|
+
if (!token) {
|
|
13
|
+
throw new Error("Expected Backstage token in sign-in response");
|
|
14
|
+
}
|
|
15
|
+
return token;
|
|
16
|
+
});
|
|
17
|
+
useMountEffect(actions.execute);
|
|
18
|
+
if (state.status === "error" && state.error) {
|
|
19
|
+
return /* @__PURE__ */ React.createElement(React.Fragment, null, "An error occurred: ", state.error.message);
|
|
20
|
+
}
|
|
21
|
+
if (state.status === "success" && state.result) {
|
|
22
|
+
return /* @__PURE__ */ React.createElement(
|
|
23
|
+
"form",
|
|
24
|
+
{
|
|
25
|
+
ref: (form) => form == null ? void 0 : form.submit(),
|
|
26
|
+
action: window.location.href,
|
|
27
|
+
method: "POST"
|
|
28
|
+
},
|
|
29
|
+
/* @__PURE__ */ React.createElement("input", { type: "hidden", name: "type", value: "sign-in" }),
|
|
30
|
+
/* @__PURE__ */ React.createElement("input", { type: "hidden", name: "token", value: state.result }),
|
|
31
|
+
/* @__PURE__ */ React.createElement("input", { type: "submit", value: "Continue" })
|
|
32
|
+
);
|
|
33
|
+
}
|
|
34
|
+
return null;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
const COOKIE_PATH = "/.backstage/auth/v1/cookie";
|
|
8
38
|
function useCookieAuthRefresh(options) {
|
|
9
|
-
const { pluginId
|
|
39
|
+
const { pluginId } = options != null ? options : {};
|
|
10
40
|
const fetchApi = useApi(fetchApiRef);
|
|
11
41
|
const discoveryApi = useApi(discoveryApiRef);
|
|
12
42
|
const channel = useMemo(() => {
|
|
@@ -14,7 +44,7 @@ function useCookieAuthRefresh(options) {
|
|
|
14
44
|
}, [pluginId]);
|
|
15
45
|
const [state, actions] = useAsync(async () => {
|
|
16
46
|
const apiOrigin = await discoveryApi.getBaseUrl(pluginId);
|
|
17
|
-
const requestUrl = `${apiOrigin}${
|
|
47
|
+
const requestUrl = `${apiOrigin}${COOKIE_PATH}`;
|
|
18
48
|
const response = await fetchApi.fetch(`${requestUrl}`, {
|
|
19
49
|
credentials: "include"
|
|
20
50
|
});
|
|
@@ -92,5 +122,5 @@ function CookieAuthRefreshProvider(props) {
|
|
|
92
122
|
return /* @__PURE__ */ React.createElement(React.Fragment, null, children);
|
|
93
123
|
}
|
|
94
124
|
|
|
95
|
-
export { CookieAuthRefreshProvider, useCookieAuthRefresh };
|
|
125
|
+
export { CookieAuthRedirect, CookieAuthRefreshProvider, useCookieAuthRefresh };
|
|
96
126
|
//# sourceMappingURL=index.esm.js.map
|
package/dist/index.esm.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.esm.js","sources":["../src/hooks/useCookieAuthRefresh/useCookieAuthRefresh.tsx","../src/components/CookieAuthRefreshProvider/CookieAuthRefreshProvider.tsx"],"sourcesContent":["/*\n * Copyright 2024 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { useEffect, useCallback, useMemo } from 'react';\nimport {\n discoveryApiRef,\n fetchApiRef,\n useApi,\n} from '@backstage/core-plugin-api';\nimport { useAsync, useMountEffect } from '@react-hookz/web';\nimport { ResponseError } from '@backstage/errors';\n\n/**\n * @public\n * A hook that will refresh the cookie when it is about to expire.\n * @param options - Options for configuring the refresh cookie endpoint\n */\nexport function useCookieAuthRefresh(options: {\n // The plugin id used for discovering the API origin\n pluginId: string;\n // The path used for calling the refresh cookie endpoint, default to '/cookie'\n path?: string;\n}):\n | { status: 'loading' }\n | { status: 'error'; error: Error; retry: () => void }\n | { status: 'success'; data: { expiresAt: string } } {\n const { pluginId, path = '/cookie' } = options ?? {};\n const fetchApi = useApi(fetchApiRef);\n const discoveryApi = useApi(discoveryApiRef);\n\n const channel = useMemo(() => {\n return 'BroadcastChannel' in window\n ? new BroadcastChannel(`${pluginId}-auth-cookie-expires-at`)\n : null;\n }, [pluginId]);\n\n const [state, actions] = useAsync<{ expiresAt: string }>(async () => {\n const apiOrigin = await discoveryApi.getBaseUrl(pluginId);\n const requestUrl = `${apiOrigin}${path}`;\n const response = await fetchApi.fetch(`${requestUrl}`, {\n credentials: 'include',\n });\n if (!response.ok) {\n throw await ResponseError.fromResponse(response);\n }\n const data = await response.json();\n if (!data.expiresAt) {\n throw new Error('No expiration date found in response');\n }\n return data;\n });\n\n useMountEffect(actions.execute);\n\n const retry = useCallback(() => {\n actions.execute();\n }, [actions]);\n\n const refresh = useCallback(\n (params: { expiresAt: string }) => {\n // Randomize the refreshing margin with a margin of 1-4 minutes to avoid all tabs refreshing at the same time\n // It cannot be less than 5 minutes otherwise the backend will return the same expiration date\n const margin = (1 + 3 * Math.random()) * 60000;\n const delay = Date.parse(params.expiresAt) - Date.now() - margin;\n const timeout = setTimeout(retry, delay);\n return () => clearTimeout(timeout);\n },\n [retry],\n );\n\n useEffect(() => {\n // Only schedule a refresh if we have a successful response\n if (state.status !== 'success' || !state.result) {\n return () => {};\n }\n channel?.postMessage({\n action: 'COOKIE_REFRESH_SUCCESS',\n payload: state.result,\n });\n let cancel = refresh(state.result);\n const listener = (\n event: MessageEvent<{ action: string; payload: { expiresAt: string } }>,\n ) => {\n const { action, payload } = event.data;\n if (action === 'COOKIE_REFRESH_SUCCESS') {\n cancel();\n cancel = refresh(payload);\n }\n };\n channel?.addEventListener('message', listener);\n return () => {\n cancel();\n channel?.removeEventListener('message', listener);\n };\n }, [state, refresh, channel]);\n\n // Initialising\n if (state.status === 'not-executed') {\n return { status: 'loading' };\n }\n\n // First refresh or retrying without any success before\n // Possible state transitions:\n // e.g. not-executed -> loading (first-refresh)\n // e.g. not-executed -> loading (first-refresh) -> error -> loading (manual-retry)\n if (state.status === 'loading' && !state.result) {\n return { status: 'loading' };\n }\n\n // Retrying after having succeeding at least once\n // Current state is: { status: 'loading', result: {...}, error: undefined | Error }\n // e.g. not-executed -> loading (first-refresh) -> success -> loading (scheduled-refresh) -> error -> loading (manual-retry)\n if (state.status === 'loading' && state.error) {\n return { status: 'loading' };\n }\n\n // Something went wrong during any situation of a refresh\n if (state.status === 'error' && state.error) {\n return { status: 'error', error: state.error, retry };\n }\n\n // At this point it should be safe to assume that we have a successful refresh\n return { status: 'success', data: state.result! };\n}\n","/*\n * Copyright 2024 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport React, { ReactNode } from 'react';\nimport { ErrorPanel } from '@backstage/core-components';\nimport { useApp } from '@backstage/core-plugin-api';\nimport { Button } from '@material-ui/core';\nimport { useCookieAuthRefresh } from '../../hooks';\n\n/**\n * @public\n * Props for the {@link CookieAuthRefreshProvider} component.\n */\nexport type CookieAuthRefreshProviderProps = {\n // The plugin ID used for discovering the API origin\n pluginId: string;\n // The path used for calling the refresh cookie endpoint, default to '/cookie'\n path?: string;\n // The children to render when the refresh is successful\n children: ReactNode;\n};\n\n/**\n * @public\n * A provider that will refresh the cookie when it is about to expire.\n */\nexport function CookieAuthRefreshProvider(\n props: CookieAuthRefreshProviderProps,\n): JSX.Element {\n const { children, ...options } = props;\n const app = useApp();\n const { Progress } = app.getComponents();\n\n const result = useCookieAuthRefresh(options);\n\n if (result.status === 'loading') {\n return <Progress />;\n }\n\n if (result.status === 'error') {\n return (\n <ErrorPanel error={result.error}>\n <Button variant=\"outlined\" onClick={result.retry}>\n Retry\n </Button>\n </ErrorPanel>\n );\n }\n\n return <>{children}</>;\n}\n"],"names":[],"mappings":";;;;;;;AA8BO,SAAS,qBAAqB,OAQkB,EAAA;AACrD,EAAA,MAAM,EAAE,QAAU,EAAA,IAAA,GAAO,SAAU,EAAA,GAAI,4BAAW,EAAC,CAAA;AACnD,EAAM,MAAA,QAAA,GAAW,OAAO,WAAW,CAAA,CAAA;AACnC,EAAM,MAAA,YAAA,GAAe,OAAO,eAAe,CAAA,CAAA;AAE3C,EAAM,MAAA,OAAA,GAAU,QAAQ,MAAM;AAC5B,IAAA,OAAO,sBAAsB,MACzB,GAAA,IAAI,iBAAiB,CAAG,EAAA,QAAQ,yBAAyB,CACzD,GAAA,IAAA,CAAA;AAAA,GACN,EAAG,CAAC,QAAQ,CAAC,CAAA,CAAA;AAEb,EAAA,MAAM,CAAC,KAAA,EAAO,OAAO,CAAA,GAAI,SAAgC,YAAY;AACnE,IAAA,MAAM,SAAY,GAAA,MAAM,YAAa,CAAA,UAAA,CAAW,QAAQ,CAAA,CAAA;AACxD,IAAA,MAAM,UAAa,GAAA,CAAA,EAAG,SAAS,CAAA,EAAG,IAAI,CAAA,CAAA,CAAA;AACtC,IAAA,MAAM,WAAW,MAAM,QAAA,CAAS,KAAM,CAAA,CAAA,EAAG,UAAU,CAAI,CAAA,EAAA;AAAA,MACrD,WAAa,EAAA,SAAA;AAAA,KACd,CAAA,CAAA;AACD,IAAI,IAAA,CAAC,SAAS,EAAI,EAAA;AAChB,MAAM,MAAA,MAAM,aAAc,CAAA,YAAA,CAAa,QAAQ,CAAA,CAAA;AAAA,KACjD;AACA,IAAM,MAAA,IAAA,GAAO,MAA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AAO,EAAE,MAAA,EAAQ,SAAW,EAAA,IAAA,EAAM,MAAM,MAAQ,EAAA,CAAA;AAClD;;ACjGO,SAAS,0BACd,KACa,EAAA;AACb,EAAA,MAAM,EAAE,QAAA,EAAU,GAAG,OAAA,EAAY,GAAA,KAAA,CAAA;AACjC,EAAA,MAAM,MAAM,MAAO,EAAA,CAAA;AACnB,EAAA,MAAM,EAAE,QAAA,EAAa,GAAA,GAAA,CAAI,aAAc,EAAA,CAAA;AAEvC,EAAM,MAAA,MAAA,GAAS,qBAAqB,OAAO,CAAA,CAAA;AAE3C,EAAI,IAAA,MAAA,CAAO,WAAW,SAAW,EAAA;AAC/B,IAAA,2CAAQ,QAAS,EAAA,IAAA,CAAA,CAAA;AAAA,GACnB;AAEA,EAAI,IAAA,MAAA,CAAO,WAAW,OAAS,EAAA;AAC7B,IAAA,uBACG,KAAA,CAAA,aAAA,CAAA,UAAA,EAAA,EAAW,KAAO,EAAA,MAAA,CAAO,KACxB,EAAA,kBAAA,KAAA,CAAA,aAAA,CAAC,MAAO,EAAA,EAAA,OAAA,EAAQ,UAAW,EAAA,OAAA,EAAS,MAAO,CAAA,KAAA,EAAA,EAAO,OAElD,CACF,CAAA,CAAA;AAAA,GAEJ;AAEA,EAAA,iEAAU,QAAS,CAAA,CAAA;AACrB;;;;"}
|
|
1
|
+
{"version":3,"file":"index.esm.js","sources":["../src/components/CookieAuthRedirect/CookieAuthRedirect.tsx","../src/hooks/useCookieAuthRefresh/useCookieAuthRefresh.tsx","../src/components/CookieAuthRefreshProvider/CookieAuthRefreshProvider.tsx"],"sourcesContent":["/*\n * Copyright 2024 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport React from 'react';\nimport { identityApiRef, useApi } from '@backstage/core-plugin-api';\nimport { useAsync, useMountEffect } from '@react-hookz/web';\n\n/**\n * @public\n * A component that redirects to the page an user was trying to access before sign-in.\n */\nexport function CookieAuthRedirect() {\n const identityApi = useApi(identityApiRef);\n\n const [state, actions] = useAsync(async () => {\n const { token } = await identityApi.getCredentials();\n if (!token) {\n throw new Error('Expected Backstage token in sign-in response');\n }\n return token;\n });\n\n useMountEffect(actions.execute);\n\n if (state.status === 'error' && state.error) {\n return <>An error occurred: {state.error.message}</>;\n }\n\n if (state.status === 'success' && state.result) {\n return (\n <form\n ref={form => form?.submit()}\n action={window.location.href}\n method=\"POST\"\n >\n <input type=\"hidden\" name=\"type\" value=\"sign-in\" />\n <input type=\"hidden\" name=\"token\" value={state.result} />\n <input type=\"submit\" value=\"Continue\" />\n </form>\n );\n }\n\n return null;\n}\n","/*\n * Copyright 2024 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport { useEffect, useCallback, useMemo } from 'react';\nimport {\n discoveryApiRef,\n fetchApiRef,\n useApi,\n} from '@backstage/core-plugin-api';\nimport { useAsync, useMountEffect } from '@react-hookz/web';\nimport { ResponseError } from '@backstage/errors';\n\nconst COOKIE_PATH = '/.backstage/auth/v1/cookie';\n\n/**\n * @public\n * A hook that will refresh the cookie when it is about to expire.\n * @param options - Options for configuring the refresh cookie endpoint\n */\nexport function useCookieAuthRefresh(options: {\n // The plugin id used for discovering the API origin\n pluginId: string;\n}):\n | { status: 'loading' }\n | { status: 'error'; error: Error; retry: () => void }\n | { status: 'success'; data: { expiresAt: string } } {\n const { pluginId } = options ?? {};\n const fetchApi = useApi(fetchApiRef);\n const discoveryApi = useApi(discoveryApiRef);\n\n const channel = useMemo(() => {\n return 'BroadcastChannel' in window\n ? new BroadcastChannel(`${pluginId}-auth-cookie-expires-at`)\n : null;\n }, [pluginId]);\n\n const [state, actions] = useAsync<{ expiresAt: string }>(async () => {\n const apiOrigin = await discoveryApi.getBaseUrl(pluginId);\n const requestUrl = `${apiOrigin}${COOKIE_PATH}`;\n const response = await fetchApi.fetch(`${requestUrl}`, {\n credentials: 'include',\n });\n if (!response.ok) {\n throw await ResponseError.fromResponse(response);\n }\n const data = await response.json();\n if (!data.expiresAt) {\n throw new Error('No expiration date found in response');\n }\n return data;\n });\n\n useMountEffect(actions.execute);\n\n const retry = useCallback(() => {\n actions.execute();\n }, [actions]);\n\n const refresh = useCallback(\n (params: { expiresAt: string }) => {\n // Randomize the refreshing margin with a margin of 1-4 minutes to avoid all tabs refreshing at the same time\n // It cannot be less than 5 minutes otherwise the backend will return the same expiration date\n const margin = (1 + 3 * Math.random()) * 60000;\n const delay = Date.parse(params.expiresAt) - Date.now() - margin;\n const timeout = setTimeout(retry, delay);\n return () => clearTimeout(timeout);\n },\n [retry],\n );\n\n useEffect(() => {\n // Only schedule a refresh if we have a successful response\n if (state.status !== 'success' || !state.result) {\n return () => {};\n }\n channel?.postMessage({\n action: 'COOKIE_REFRESH_SUCCESS',\n payload: state.result,\n });\n let cancel = refresh(state.result);\n const listener = (\n event: MessageEvent<{ action: string; payload: { expiresAt: string } }>,\n ) => {\n const { action, payload } = event.data;\n if (action === 'COOKIE_REFRESH_SUCCESS') {\n cancel();\n cancel = refresh(payload);\n }\n };\n channel?.addEventListener('message', listener);\n return () => {\n cancel();\n channel?.removeEventListener('message', listener);\n };\n }, [state, refresh, channel]);\n\n // Initialising\n if (state.status === 'not-executed') {\n return { status: 'loading' };\n }\n\n // First refresh or retrying without any success before\n // Possible state transitions:\n // e.g. not-executed -> loading (first-refresh)\n // e.g. not-executed -> loading (first-refresh) -> error -> loading (manual-retry)\n if (state.status === 'loading' && !state.result) {\n return { status: 'loading' };\n }\n\n // Retrying after having succeeding at least once\n // Current state is: { status: 'loading', result: {...}, error: undefined | Error }\n // e.g. not-executed -> loading (first-refresh) -> success -> loading (scheduled-refresh) -> error -> loading (manual-retry)\n if (state.status === 'loading' && state.error) {\n return { status: 'loading' };\n }\n\n // Something went wrong during any situation of a refresh\n if (state.status === 'error' && state.error) {\n return { status: 'error', error: state.error, retry };\n }\n\n // At this point it should be safe to assume that we have a successful refresh\n return { status: 'success', data: state.result! };\n}\n","/*\n * Copyright 2024 The Backstage Authors\n *\n * Licensed under the Apache License, Version 2.0 (the \"License\");\n * you may not use this file except in compliance with the License.\n * You may obtain a copy of the License at\n *\n * http://www.apache.org/licenses/LICENSE-2.0\n *\n * Unless required by applicable law or agreed to in writing, software\n * distributed under the License is distributed on an \"AS IS\" BASIS,\n * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n * See the License for the specific language governing permissions and\n * limitations under the License.\n */\n\nimport React, { ReactNode } from 'react';\nimport { ErrorPanel } from '@backstage/core-components';\nimport { useApp } from '@backstage/core-plugin-api';\nimport Button from '@material-ui/core/Button';\nimport { useCookieAuthRefresh } from '../../hooks';\n\n/**\n * @public\n * Props for the {@link CookieAuthRefreshProvider} component.\n */\nexport type CookieAuthRefreshProviderProps = {\n // The plugin ID used for discovering the API origin\n pluginId: string;\n // The children to render when the refresh is successful\n children: ReactNode;\n};\n\n/**\n * @public\n * A provider that will refresh the cookie when it is about to expire.\n */\nexport function CookieAuthRefreshProvider(\n props: CookieAuthRefreshProviderProps,\n): JSX.Element {\n const { children, ...options } = props;\n const app = useApp();\n const { Progress } = app.getComponents();\n\n const result = useCookieAuthRefresh(options);\n\n if (result.status === 'loading') {\n return <Progress />;\n }\n\n if (result.status === 'error') {\n return (\n <ErrorPanel error={result.error}>\n <Button variant=\"outlined\"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AAO,EAAE,MAAA,EAAQ,SAAW,EAAA,IAAA,EAAM,MAAM,MAAQ,EAAA,CAAA;AAClD;;ACnGO,SAAS,0BACd,KACa,EAAA;AACb,EAAA,MAAM,EAAE,QAAA,EAAU,GAAG,OAAA,EAAY,GAAA,KAAA,CAAA;AACjC,EAAA,MAAM,MAAM,MAAO,EAAA,CAAA;AACnB,EAAA,MAAM,EAAE,QAAA,EAAa,GAAA,GAAA,CAAI,aAAc,EAAA,CAAA;AAEvC,EAAM,MAAA,MAAA,GAAS,qBAAqB,OAAO,CAAA,CAAA;AAE3C,EAAI,IAAA,MAAA,CAAO,WAAW,SAAW,EAAA;AAC/B,IAAA,2CAAQ,QAAS,EAAA,IAAA,CAAA,CAAA;AAAA,GACnB;AAEA,EAAI,IAAA,MAAA,CAAO,WAAW,OAAS,EAAA;AAC7B,IAAA,uBACG,KAAA,CAAA,aAAA,CAAA,UAAA,EAAA,EAAW,KAAO,EAAA,MAAA,CAAO,KACxB,EAAA,kBAAA,KAAA,CAAA,aAAA,CAAC,MAAO,EAAA,EAAA,OAAA,EAAQ,UAAW,EAAA,OAAA,EAAS,MAAO,CAAA,KAAA,EAAA,EAAO,OAElD,CACF,CAAA,CAAA;AAAA,GAEJ;AAEA,EAAA,iEAAU,QAAS,CAAA,CAAA;AACrB;;;;"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@backstage/plugin-auth-react",
|
|
3
|
-
"version": "0.0.0-nightly-
|
|
3
|
+
"version": "0.0.0-nightly-20240412021259",
|
|
4
4
|
"description": "Web library for the auth plugin",
|
|
5
5
|
"backstage": {
|
|
6
6
|
"role": "web-library"
|
|
@@ -32,7 +32,7 @@
|
|
|
32
32
|
"test": "backstage-cli package test"
|
|
33
33
|
},
|
|
34
34
|
"dependencies": {
|
|
35
|
-
"@backstage/core-components": "^0.0.0-nightly-
|
|
35
|
+
"@backstage/core-components": "^0.0.0-nightly-20240412021259",
|
|
36
36
|
"@backstage/core-plugin-api": "^1.9.1",
|
|
37
37
|
"@backstage/errors": "^1.2.4",
|
|
38
38
|
"@material-ui/core": "^4.9.13",
|
|
@@ -40,11 +40,12 @@
|
|
|
40
40
|
"@types/react": "^16.13.1 || ^17.0.0 || ^18.0.0"
|
|
41
41
|
},
|
|
42
42
|
"devDependencies": {
|
|
43
|
-
"@backstage/cli": "^0.0.0-nightly-
|
|
44
|
-
"@backstage/test-utils": "^
|
|
43
|
+
"@backstage/cli": "^0.0.0-nightly-20240412021259",
|
|
44
|
+
"@backstage/test-utils": "^0.0.0-nightly-20240412021259",
|
|
45
45
|
"@testing-library/jest-dom": "^6.0.0",
|
|
46
46
|
"@testing-library/react": "^14.0.0",
|
|
47
|
-
"@testing-library/user-event": "^14.0.0"
|
|
47
|
+
"@testing-library/user-event": "^14.0.0",
|
|
48
|
+
"msw": "^1.0.0"
|
|
48
49
|
},
|
|
49
50
|
"peerDependencies": {
|
|
50
51
|
"react": "^16.13.1 || ^17.0.0 || ^18.0.0"
|