Skip to content

Commit

Permalink
fix: add auth header to public image upload (#2496)
Browse files Browse the repository at this point in the history
- The public file upload route requires auth to allow teamEditors to upload images
  • Loading branch information
Mike-Heneghan authored Nov 29, 2023
1 parent a572587 commit ddd9ca4
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions editor.planx.uk/src/api/upload.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import axios from "axios";
import axios, { RawAxiosRequestHeaders } from "axios";
import { getCookie } from "lib/cookie";

export { uploadPrivateFile, uploadPublicFile };

Expand All @@ -8,7 +9,13 @@ async function uploadPublicFile(
file: any,
{ onProgress }: { onProgress?: (p: any) => void } = {},
) {
const { data } = await handleUpload(file, { onProgress, path: "public" });
const token = getCookie("jwt");
const authRequestHeader = { Authorization: `Bearer ${token}` };
const { data } = await handleUpload(
file,
{ onProgress, path: "public" },
authRequestHeader,
);

return data.fileUrl;
}
Expand All @@ -28,6 +35,7 @@ function handleUpload(
onProgress,
path: path,
}: { onProgress?: (p: any) => void; path: "public" | "private" },
authHeader?: RawAxiosRequestHeaders,
) {
const formData = new FormData();

Expand All @@ -45,6 +53,7 @@ function handleUpload(
return axios.post(endpoint, formData, {
headers: {
"Content-Type": "multipart/form-data",
...(authHeader && authHeader),
},
onUploadProgress: ({ loaded, total }) => {
if (onProgress && total) {
Expand Down

0 comments on commit ddd9ca4

Please sign in to comment.