Skip to content

Commit 286e529

Browse files
Adds authentication token to collection requests (#51)
Ensures that the authentication token is included in the headers of requests made to create or update collections. Fix #29
1 parent aaaa83a commit 286e529

File tree

1 file changed

+14
-6
lines changed
  • packages/client/src/pages/CollectionForm

1 file changed

+14
-6
lines changed

packages/client/src/pages/CollectionForm/index.tsx

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ import { useNavigate, useParams } from 'react-router-dom';
55
import { useCollection } from '@developmentseed/stac-react';
66
import { StacCollection } from 'stac-ts';
77

8-
import usePageTitle from '$hooks/usePageTitle';
9-
import Api from 'src/api';
8+
import Api from '../../api';
9+
import { useKeycloak } from '../../auth/Context';
1010
import { EditForm } from './EditForm';
11+
import usePageTitle from '$hooks/usePageTitle';
1112
import {
1213
AppNotification,
1314
parseResponseForNotifications
@@ -32,6 +33,8 @@ export function CollectionFormNew() {
3233
AppNotification[] | undefined
3334
>();
3435

36+
const { keycloak } = useKeycloak();
37+
3538
const onSubmit = async (data: any, formikHelpers: FormikHelpers<any>) => {
3639
try {
3740
toast.closeAll();
@@ -44,7 +47,7 @@ export function CollectionFormNew() {
4447
position: 'bottom-right'
4548
});
4649

47-
await collectionTransaction().create(data);
50+
await collectionTransaction(keycloak?.token).create(data);
4851

4952
toast.update('collection-submit', {
5053
title: 'Collection created',
@@ -78,6 +81,8 @@ export function CollectionFormEdit(props: { id: string }) {
7881

7982
const toast = useToast();
8083

84+
const { keycloak } = useKeycloak();
85+
8186
useEffect(() => {
8287
if (state === 'LOADING') {
8388
setTriedLoading(true);
@@ -103,7 +108,7 @@ export function CollectionFormEdit(props: { id: string }) {
103108
duration: null,
104109
position: 'bottom-right'
105110
});
106-
await collectionTransaction().update(id, data);
111+
await collectionTransaction(keycloak?.token).update(id, data);
107112

108113
toast.update('collection-submit', {
109114
title: 'Collection updated',
@@ -134,15 +139,18 @@ type collectionTransactionType = {
134139
create: (data: StacCollection) => Promise<StacCollection>;
135140
};
136141

137-
function collectionTransaction(): collectionTransactionType {
142+
function collectionTransaction(token?: string): collectionTransactionType {
138143
const createRequest = async (
139144
url: string,
140145
method: string,
141146
data: StacCollection
142147
) => {
143148
return Api.fetch(url, {
144149
method,
145-
headers: { 'Content-Type': 'application/json' },
150+
headers: {
151+
'Content-Type': 'application/json',
152+
Authorization: token ? `Bearer ${token}` : undefined
153+
},
146154
body: JSON.stringify(data)
147155
});
148156
};

0 commit comments

Comments
 (0)