From 558488cf0ebd186d40ba4d2c61cdac1375e7ca55 Mon Sep 17 00:00:00 2001 From: Daniel da Silva Date: Thu, 3 Jul 2025 11:47:05 +0100 Subject: [PATCH] Adds authentication token to collection requests Ensures that the authentication token is included in the headers of requests made to create or update collections. Fix #29 --- .../client/src/pages/CollectionForm/index.tsx | 20 +++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/packages/client/src/pages/CollectionForm/index.tsx b/packages/client/src/pages/CollectionForm/index.tsx index 1185501..9a86f09 100644 --- a/packages/client/src/pages/CollectionForm/index.tsx +++ b/packages/client/src/pages/CollectionForm/index.tsx @@ -5,9 +5,10 @@ import { useNavigate, useParams } from 'react-router-dom'; import { useCollection } from '@developmentseed/stac-react'; import { StacCollection } from 'stac-ts'; -import usePageTitle from '$hooks/usePageTitle'; -import Api from 'src/api'; +import Api from '../../api'; +import { useKeycloak } from '../../auth/Context'; import { EditForm } from './EditForm'; +import usePageTitle from '$hooks/usePageTitle'; import { AppNotification, parseResponseForNotifications @@ -32,6 +33,8 @@ export function CollectionFormNew() { AppNotification[] | undefined >(); + const { keycloak } = useKeycloak(); + const onSubmit = async (data: any, formikHelpers: FormikHelpers) => { try { toast.closeAll(); @@ -44,7 +47,7 @@ export function CollectionFormNew() { position: 'bottom-right' }); - await collectionTransaction().create(data); + await collectionTransaction(keycloak?.token).create(data); toast.update('collection-submit', { title: 'Collection created', @@ -78,6 +81,8 @@ export function CollectionFormEdit(props: { id: string }) { const toast = useToast(); + const { keycloak } = useKeycloak(); + useEffect(() => { if (state === 'LOADING') { setTriedLoading(true); @@ -103,7 +108,7 @@ export function CollectionFormEdit(props: { id: string }) { duration: null, position: 'bottom-right' }); - await collectionTransaction().update(id, data); + await collectionTransaction(keycloak?.token).update(id, data); toast.update('collection-submit', { title: 'Collection updated', @@ -134,7 +139,7 @@ type collectionTransactionType = { create: (data: StacCollection) => Promise; }; -function collectionTransaction(): collectionTransactionType { +function collectionTransaction(token?: string): collectionTransactionType { const createRequest = async ( url: string, method: string, @@ -142,7 +147,10 @@ function collectionTransaction(): collectionTransactionType { ) => { return Api.fetch(url, { method, - headers: { 'Content-Type': 'application/json' }, + headers: { + 'Content-Type': 'application/json', + Authorization: token ? `Bearer ${token}` : undefined + }, body: JSON.stringify(data) }); };