UNPKG

vfi-2fa

Version:

- Headless (100% customizable, Bring-your-own-UI) - Auto out of the box, fully controllable API - Sorting (Multi and Stable) - Filters - Row Selection - Row Expansion - Column Ordering - Animatable - Resizable - Server-side/controlled data/state

100 lines (99 loc) 4.24 kB
import axios from 'axios'; import qs from 'qs'; export const axiosInstance = axios.create({ timeout: 30000, withCredentials: true }); axiosInstance.interceptors.response.use(response => { return response; }, async (error) => { // if (error?.code === "ECONNABORTED") { // return error; // } if (error.response?.status === 403) { try { let errorResponse = error.response.data; if (errorResponse.data?.required2FA) { let currentErrorConfig = error.config; let isResolve = false; do { const otpRequestEvent = new CustomEvent("otp-required", { detail: { errorConfig: error.config, otpData: errorResponse.data } }); document.dispatchEvent(otpRequestEvent); const otpInput = await new Promise((resolve) => { const handleOtpSuccess = (e) => { const otpDetails = e.detail; resolve(otpDetails); document.removeEventListener("otp-success", handleOtpSuccess); // Xóa listener sau khi đã xử lý }; document.addEventListener("otp-success", handleOtpSuccess); }); if (otpInput.isCancel) { isResolve = true; return Promise.reject({ error: { isValid: false, errors: [ { errorMessage: "Thao tác không thành công do không xác thực được OTP" } ] } }); } // Sau khi nhận được OTP hợp lệ từ sự kiện, thực hiện lại request // Đặt lại cấu hình hoặc gửi request lại tùy theo trường hợp try { const retryResponse = await axios({ ...currentErrorConfig, // Sử dụng cấu hình yêu cầu gốc headers: { ...currentErrorConfig?.headers, 'OTP': otpInput.otp, // Thêm OTP vào header 'Secret': otpInput.secret, // Thêm Secret vào header nếu có 'otp-type': otpInput.type // Thêm Type vào header nếu có } }); isResolve = true; const hideModalEvent = new CustomEvent("otp-modal-hide", {}); document.dispatchEvent(hideModalEvent); return retryResponse; // Hoặc trả về kết quả cần thiết } catch (retryError) { if (axios.isAxiosError(retryError)) { currentErrorConfig = retryError.config; errorResponse = retryError.response?.data; } } } while (!isResolve); } } catch (errorCatch) { console.log("🚀 ~ errorCatch:", errorCatch); return; } } if (error?.code === 'ERR_BAD_REQUEST') { if (error.response?.data) { return Promise.reject(error.response?.data); } } if (error?.request?.responseURL) { console.log(error.request.responseURL); } else { if (error?.request?.host) { console.log(`${error.request.protocol}//${error.request.host}${error.request.path}`); } } return Promise.reject(error.message); }); axiosInstance.interceptors.request.use(config => { config.paramsSerializer = (params) => { return qs.stringify(params, { indices: false }); }; return config; });