Skip to content

Commit 3872ed2

Browse files
committed
Task EPAM-JS-Competency-center#7 Authorization
- add alerts on unauthenticated requests
1 parent 161a70a commit 3872ed2

File tree

3 files changed

+32
-5
lines changed

3 files changed

+32
-5
lines changed

src/components/pages/admin/PageProductImport/components/CSVFileImport.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,24 +25,30 @@ export default function CSVFileImport({ url, title }: CSVFileImportProps) {
2525

2626
const uploadFile = async () => {
2727
console.log("uploadFile to", url);
28-
29-
// Get the presigned URL
28+
if (!file) {
29+
console.error("File not specified");
30+
return;
31+
}
32+
// Get the pre-signed URL
3033
const response = await axios({
3134
method: "GET",
35+
headers: { Authorization: "Basic ddm9yb3Noa292OlRFU1RfUEFTU1dPUkQ=" },
3236
url,
3337
params: {
3438
name: encodeURIComponent(file.name),
3539
},
3640
});
3741
console.log("File to upload: ", file.name);
3842
console.log("Uploading to: ", response.data);
43+
3944
const result = await fetch(response.data, {
4045
method: "PUT",
4146
body: file,
4247
});
4348
console.log("Result: ", result);
44-
setFile("");
49+
setFile(undefined);
4550
};
51+
4652
return (
4753
<Box>
4854
<Typography variant="h6" gutterBottom>

src/constants/apiPaths.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
const API_PATHS = {
2-
product: "https://7u8vlssv4d.execute-api.us-east-1.amazonaws.com",
2+
product: "https://bsar9vq69i.execute-api.us-east-1.amazonaws.com",
33
order: "https://.execute-api.eu-west-1.amazonaws.com/dev",
44
import: "https://ol9cna0j4c.execute-api.us-east-1.amazonaws.com/dev",
5-
bff: "https://7u8vlssv4d.execute-api.us-east-1.amazonaws.com",
5+
bff: "https://bsar9vq69i.execute-api.us-east-1.amazonaws.com",
66
cart: "https://.execute-api.eu-west-1.amazonaws.com/dev",
77
};
88

src/index.tsx

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import axios from "axios";
12
import React from "react";
23
import { createRoot } from "react-dom/client";
34
import App from "~/components/App/App";
@@ -18,6 +19,26 @@ if (import.meta.env.DEV) {
1819
const { worker } = await import("./mocks/browser");
1920
worker.start({ onUnhandledRequest: "bypass" });
2021
}
22+
axios.interceptors.response.use(
23+
(response) => response,
24+
(error) => {
25+
const { status } = error.response;
26+
if (status === 401) {
27+
alert(`Current operation requires authorization`);
28+
}
29+
if (status === 403) {
30+
alert(`You are not authorized to perform this operation`);
31+
}
32+
return Promise.reject(error.response);
33+
}
34+
);
35+
36+
if (!localStorage.getItem("authorization_token")) {
37+
localStorage.setItem(
38+
"authorization_token",
39+
"VmljdG9yQmVsaWtvdjpURVNUX1BBU1NXT1JE"
40+
);
41+
}
2142

2243
const container = document.getElementById("app");
2344
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion

0 commit comments

Comments
 (0)