app-backend auth review fixes

Signed-off-by: Patrik Oldsberg <poldsberg@gmail.com>
This commit is contained in:
Patrik Oldsberg
2024-04-09 15:51:29 +02:00
parent 30c7ed4bad
commit d1adc89efc
7 changed files with 11 additions and 16 deletions
@@ -34,7 +34,7 @@ describe('CookieAuthRefreshProvider', () => {
const discoveryApiMock = {
getBaseUrl: jest
.fn()
.mockResolvedValue('http://localhost:7000/techdocs/api'),
.mockResolvedValue('http://localhost:7000/api/techdocs'),
};
function getExpiresAtInFuture() {
@@ -119,7 +119,7 @@ describe('CookieAuthRefreshProvider', () => {
await waitFor(() =>
expect(fetchApiMock.fetch).toHaveBeenCalledWith(
'http://localhost:7000/techdocs/api/.backstage/auth/v1/cookie',
'http://localhost:7000/api/techdocs/.backstage/auth/v1/cookie',
{ credentials: 'include' },
),
);
@@ -27,7 +27,7 @@ import { useCookieAuthRefresh } from '../../hooks';
export type CookieAuthRefreshProviderProps = {
// The plugin ID used for discovering the API origin
pluginId: string;
// The path used for calling the refresh cookie endpoint, default to '/cookie'
// The path used for calling the refresh cookie endpoint, default to '/.backstage/auth/v1/cookie'
path?: string;
// The children to render when the refresh is successful
children: ReactNode;
@@ -24,7 +24,7 @@ describe('useCookieAuthRefresh', () => {
const discoveryApiMock = {
getBaseUrl: jest
.fn()
.mockResolvedValue('http://localhost:7000/techdocs/api'),
.mockResolvedValue('http://localhost:7000/api/techdocs'),
};
const now = 1710316886171;
@@ -236,7 +236,7 @@ describe('useCookieAuthRefresh', () => {
await waitFor(() =>
expect(fetchApiMock.fetch).toHaveBeenCalledWith(
'http://localhost:7000/techdocs/api/.backstage/auth/v1/cookie',
'http://localhost:7000/api/techdocs/.backstage/auth/v1/cookie',
{ credentials: 'include' },
),
);
@@ -23,6 +23,8 @@ import {
import { useAsync, useMountEffect } from '@react-hookz/web';
import { ResponseError } from '@backstage/errors';
const COOKIE_PATH = '/.backstage/auth/v1/cookie';
/**
* @public
* A hook that will refresh the cookie when it is about to expire.
@@ -31,13 +33,11 @@ import { ResponseError } from '@backstage/errors';
export function useCookieAuthRefresh(options: {
// The plugin id used for discovering the API origin
pluginId: string;
// The path used for calling the refresh cookie endpoint, default to '/.backstage/auth/v1/cookie'
path?: string;
}):
| { status: 'loading' }
| { status: 'error'; error: Error; retry: () => void }
| { status: 'success'; data: { expiresAt: string } } {
const { pluginId, path = '/.backstage/auth/v1/cookie' } = options ?? {};
const { pluginId } = options ?? {};
const fetchApi = useApi(fetchApiRef);
const discoveryApi = useApi(discoveryApiRef);
@@ -49,7 +49,7 @@ export function useCookieAuthRefresh(options: {
const [state, actions] = useAsync<{ expiresAt: string }>(async () => {
const apiOrigin = await discoveryApi.getBaseUrl(pluginId);
const requestUrl = `${apiOrigin}${path}`;
const requestUrl = `${apiOrigin}${COOKIE_PATH}`;
const response = await fetchApi.fetch(`${requestUrl}`, {
credentials: 'include',
});