Skip to content

Commit

Permalink
Merge pull request #47 from AAISS/fix/add-item-to-cart
Browse files Browse the repository at this point in the history
fix: show error to unauthorized user trying to add item to cart
  • Loading branch information
KimiaMontazeri authored Nov 30, 2023
2 parents 965566b + 6c298a5 commit aa287f3
Showing 1 changed file with 24 additions and 19 deletions.
43 changes: 24 additions & 19 deletions frontend/src/providers/APIProvider/APIProvider.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -172,22 +172,27 @@ export function APIProvider({ children }) {
break;
}

const tokenStr = JSON.parse(localStorage.getItem('user'))['access'];
await service
.post(`${URL.baseURL}${URL.services.default}${endpoint}`, body, {
headers: { Authorization: `Bearer ${tokenStr}` },
})
.then((response) => {
setAddToCartResponse(response);
})
.catch((error) => {
setAddToCartResponse(error.response);
if (!error) return;

if (error.response.status === 401) {
updateAccessTokenWithRefreshToken();
}
});
const userKey = JSON.parse(localStorage.getItem('user'));
if (userKey == null) {
setAddToCartResponse({ status: 401 });
} else {
const tokenStr = userKey.access;
await service
.post(`${URL.baseURL}${URL.services.default}${endpoint}`, body, {
headers: { Authorization: `Bearer ${tokenStr}` },
})
.then((response) => {
setAddToCartResponse(response);
})
.catch((error) => {
setAddToCartResponse(error.response);
if (!error) return;

if (error.response.status === 401) {
updateAccessTokenWithRefreshToken();
}
});
}
},
[service],
);
Expand Down Expand Up @@ -235,9 +240,9 @@ export function APIProvider({ children }) {
.then((response) => {
setPaymentData(response);
})
.catch(error => {
setPaymentData(error.response)
});
.catch((error) => {
setPaymentData(error.response);
});
},
[currentYear, service, getAccessTokenHeader],
);
Expand Down

0 comments on commit aa287f3

Please sign in to comment.