fix(auth,checkout): add Axios 401/403 auto-logout response interceptor and fix charity round-up 10k exact math

This commit is contained in:
parsa aghaei committed 2026-07-26 20:23:27 +03:30
1 parent 72badb1a7b
commit bdf8856883
3 files changed
+86 -8

No files matched your search

@@ -61,12 +61,12 @@ export default function CheckoutPage() {
}, [profile, isLoggedIn]);
const subtotal = getSubtotal();
const roundedAmount = Math.ceil(subtotal / 10000) * 10000;
const roundUpDiff = roundedAmount - subtotal;
const remainder = subtotal % 10000;
const roundUpDiff = remainder === 0 ? 10000 : 10000 - remainder;
const handleCharityToggle = () => {
if (!useRoundUp) {
setCharityDonation(Math.max(roundUpDiff, Math.round(subtotal * 0.01)));
setCharityDonation(roundUpDiff);
setUseRoundUp(true);
} else {
setCharityDonation(0);
+22 -1
View File
@@ -10,7 +10,7 @@ const api = axios.create({
},
});
// Interceptor to add auth token in the future
// Interceptor to add auth token in request headers
api.interceptors.request.use(
(config) => {
let token = null;
@@ -25,4 +25,25 @@ api.interceptors.request.use(
(error) => Promise.reject(error)
);
// Interceptor to handle 401/403 token expiration globally
api.interceptors.response.use(
(response) => response,
(error) => {
if (error.response && (error.response.status === 401 || error.response.status === 403)) {
if (typeof window !== 'undefined') {
localStorage.removeItem('accessToken');
localStorage.removeItem('refreshToken');
try {
// Dynamic import to avoid circular dependency
const { useUserStore } = require('../store/userStore');
useUserStore.getState().logout();
} catch {
// Ignore if store not initialized
}
}
}
return Promise.reject(error);
}
);
export default api;