Security: - Disable SSL verification only in development (lib/req.js) - Wrap all JSON.parse(cookie) calls in try-catch via safeJsonParse utility - Sanitize dangerouslySetInnerHTML in blog/clinic with sanitizeHtml utility - Fix open redirect in payment page — validate URL origin before redirect - Fix cookie cleanup on 401 — use js-cookie with correct domain scope Performance: - Wrap ItemDoctor with React.memo to prevent unnecessary re-renders - Replace <img> with Next.js <Image> in blog Caption component Functionality: - Fix memory leak in Recode.js — store intervals in refs, cleanup on unmount - Add null guard on retryIcon.current before classList manipulation - Fix getParsedUserInfo in helper to handle malformed cookie gracefully Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
52 lines
1.4 KiB
JavaScript
52 lines
1.4 KiB
JavaScript
import axios from "axios";
|
|
import Cookies from "js-cookie";
|
|
|
|
const BASE_URL = process.env.NEXT_PUBLIC_API_URL;
|
|
|
|
const api = axios.create({
|
|
baseURL: BASE_URL,
|
|
"X-CSRF-Token": "",
|
|
headers: {
|
|
"Content-Type": "application/json",
|
|
},
|
|
});
|
|
|
|
// اضافه کردن interceptor برای افزودن token به ریکوستهایی که نیاز به احراز هویت دارند
|
|
api.interceptors.request.use(
|
|
(config) => {
|
|
// فقط اگر requireAuth در config تنظیم شده باشد، token اضافه میشود
|
|
if (config.requireAuth) {
|
|
const token = Cookies.get("access_token");
|
|
if (token) {
|
|
config.headers.Authorization = `Bearer ${token}`;
|
|
}
|
|
}
|
|
return config;
|
|
},
|
|
(error) => {
|
|
return Promise.reject(error);
|
|
}
|
|
);
|
|
|
|
api.interceptors.response.use(
|
|
(response) => {
|
|
return response.data;
|
|
},
|
|
(error) => {
|
|
if (error?.response?.status === 401 && typeof window !== "undefined") {
|
|
const hostname = window.location.hostname;
|
|
const domain = hostname.includes("localhost")
|
|
? undefined
|
|
: `.${hostname.split(".").slice(-2).join(".")}`;
|
|
const opts = { path: "/", ...(domain && { domain }) };
|
|
["access_token", "refresh_token", "uuid", "userInfo"].forEach((key) => {
|
|
Cookies.remove(key, opts);
|
|
});
|
|
window.location.href = "/login";
|
|
}
|
|
return Promise.reject(error);
|
|
}
|
|
);
|
|
|
|
export default api;
|