refactor: move redirect to root

Signed-off-by: Camila Belo <camilaibs@gmail.com>
This commit is contained in:
Camila Belo
2024-03-19 19:41:50 +01:00
committed by Patrik Oldsberg
parent f17da85b08
commit 60167a3980
8 changed files with 147 additions and 33 deletions
+4
View File
@@ -3,6 +3,7 @@
> Do not edit this file. It is a report generated by [API Extractor](https://api-extractor.com/).
```ts
import { default as React_2 } from 'react';
import { ReactNode } from 'react';
// @public
@@ -17,6 +18,9 @@ export type CookieAuthRefreshProviderProps = {
children: ReactNode;
};
// @public
export function RedirectToRoot(): React_2.JSX.Element | null;
// @public
export function useCookieAuthRefresh(options: {
pluginId: string;
@@ -0,0 +1,64 @@
/*
* Copyright 2024 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import React from 'react';
import { screen, waitFor } from '@testing-library/react';
import { TestApiProvider, renderInTestApp } from '@backstage/test-utils';
import { identityApiRef } from '@backstage/core-plugin-api';
import { RedirectToRoot } from './RedirectToRoot';
describe('RedirectToRoot', () => {
const identityApiMock = { getCredentials: jest.fn() };
beforeEach(() => {
jest.clearAllMocks();
});
it('should render an error message if token is not available', async () => {
identityApiMock.getCredentials.mockResolvedValue({ token: undefined });
await renderInTestApp(
<TestApiProvider apis={[[identityApiRef, identityApiMock]]}>
<RedirectToRoot />
</TestApiProvider>,
);
await waitFor(() =>
expect(
screen.getByText(
'An error occurred: Expected Backstage token in sign-in response',
),
).toBeInTheDocument(),
);
});
it('should render a form with token if token is available', async () => {
identityApiMock.getCredentials.mockResolvedValue({ token: 'test-token' });
await renderInTestApp(
<TestApiProvider apis={[[identityApiRef, identityApiMock]]}>
<RedirectToRoot />
</TestApiProvider>,
);
await waitFor(() =>
expect(screen.getByText('Continue')).toBeInTheDocument(),
);
expect(screen.getByDisplayValue('sign-in')).toBeInTheDocument();
expect(screen.getByDisplayValue('test-token')).toBeInTheDocument();
});
});
@@ -0,0 +1,57 @@
/*
* Copyright 2024 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import React from 'react';
import { identityApiRef, useApi } from '@backstage/core-plugin-api';
import { useAsync, useMountEffect } from '@react-hookz/web';
/**
* @public
* A component that redirects to the root of the app after a successful sign-in.
*/
export function RedirectToRoot() {
const identityApi = useApi(identityApiRef);
const [state, actions] = useAsync(async () => {
const { token } = await identityApi.getCredentials();
if (!token) {
throw new Error('Expected Backstage token in sign-in response');
}
return token;
});
useMountEffect(actions.execute);
if (state.status === 'error' && state.error) {
return <>An error occurred: {state.error.message}</>;
}
if (state.status === 'success' && state.result) {
return (
<form
ref={form => form?.submit()}
action={window.location.href}
method="POST"
>
<input type="hidden" name="type" value="sign-in" />
<input type="hidden" name="token" value={state.result} />
<input type="submit" value="Continue" />
</form>
);
}
return null;
}
@@ -0,0 +1,17 @@
/*
* Copyright 2024 The Backstage Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
export { RedirectToRoot } from './RedirectToRoot';
@@ -17,4 +17,5 @@
// The index file in ./components/ is typically responsible for selecting
// which components are public API and should be exported from the package.
export * from './RedirectToRoot';
export * from './CookieAuthRefreshProvider';