diff --git a/package.json b/package.json index 1ee8caba..c20d2d91 100644 --- a/package.json +++ b/package.json @@ -25,6 +25,7 @@ "add": "^2.0.6", "axios": "^1.2.6", "clsx": "^1.2.1", + "cors": "^2.8.5", "crypto-js": "^4.1.1", "dayjs": "^1.11.9", "dotenv": "^16.3.1", @@ -59,6 +60,7 @@ "zod": "^3.20.6" }, "devDependencies": { + "@types/cors": "^2.8.13", "@types/crypto-js": "^4.1.1", "@types/geoip-country": "^4.0.0", "@types/mixpanel-browser": "^2.38.1", diff --git a/src/api/axios.ts b/src/api/axios.ts index 2f67fcc7..65bc7eaf 100644 --- a/src/api/axios.ts +++ b/src/api/axios.ts @@ -1,6 +1,7 @@ import axios from 'axios'; +import Cors from 'cors'; import { NextApiRequest, NextApiResponse } from 'next'; -import { BASE_URL } from '../types/constants'; +import { BASE_URL, BASE_URL_SHORT, alternateBrandUrl } from '../types/constants'; export const API = axios.create({ baseURL: BASE_URL, @@ -22,17 +23,17 @@ export function withAuth(token?: string) { }; } +const cors = Cors({ + origin: [BASE_URL, BASE_URL_SHORT, ...alternateBrandUrl], + methods: ['GET', 'POST', 'PUT', 'PATCH'], +}); + export const allowCors = (handler: any) => async (req: NextApiRequest, res: NextApiResponse) => { - res.setHeader('Access-Control-Allow-Credentials', 'true'); - res.setHeader('Access-Control-Allow-Origin', '*'); - res.setHeader('Access-Control-Allow-Methods', 'GET,OPTIONS,PATCH,DELETE,POST,PUT'); - res.setHeader( - 'Access-Control-Allow-Headers', - 'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version', - ); if (req.method === 'OPTIONS') { res.status(200).end(); return; } - return await handler(req, res); + return cors(req, res, async () => { + return await handler(req, res); + }); }; diff --git a/src/types/constants.ts b/src/types/constants.ts index b4b167ef..6108bc56 100644 --- a/src/types/constants.ts +++ b/src/types/constants.ts @@ -2,6 +2,7 @@ export const localUrl = 'http://localhost:5000'; export const brandUrl = 'https://clickdi.top'; export const brandUrlShort = 'https://clid.top'; export const brandUrlShortDomain = 'clid.top'; +export const alternateBrandUrl = ['https://vietnamese.cloud']; export const isProduction = process.env.NODE_ENV === 'production'; export const isTest = process.env.NODE_ENV === 'test'; export const cdnUrl = 'https://cdn.jsdelivr.net/gh/thanhdanh27600/clickdi@production/public'; diff --git a/yarn.lock b/yarn.lock index 05108bde..1ac08e82 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1168,6 +1168,13 @@ dependencies: "@babel/types" "^7.20.7" +"@types/cors@^2.8.13": + version "2.8.13" + resolved "https://registry.yarnpkg.com/@types/cors/-/cors-2.8.13.tgz#b8ade22ba455a1b8cb3b5d3f35910fd204f84f94" + integrity sha512-RG8AStHlUiV5ysZQKq97copd2UmVYw3/pRMLefISZ3S1hK104Cwm7iLQ3fTKx+lsUH2CE8FlLaYeEA2LSeqYUA== + dependencies: + "@types/node" "*" + "@types/crypto-js@^4.1.1": version "4.1.1" resolved "https://registry.yarnpkg.com/@types/crypto-js/-/crypto-js-4.1.1.tgz#602859584cecc91894eb23a4892f38cfa927890d" @@ -2024,6 +2031,14 @@ core-js@^3: resolved "https://registry.yarnpkg.com/core-js/-/core-js-3.28.0.tgz#ed8b9e99c273879fdfff0edfc77ee709a5800e4a" integrity sha512-GiZn9D4Z/rSYvTeg1ljAIsEqFm0LaN9gVtwDCrKL80zHtS31p9BAjmTxVqTQDMpwlMolJZOFntUG2uwyj7DAqw== +cors@^2.8.5: + version "2.8.5" + resolved "https://registry.yarnpkg.com/cors/-/cors-2.8.5.tgz#eac11da51592dd86b9f06f6e7ac293b3df875d29" + integrity sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g== + dependencies: + object-assign "^4" + vary "^1" + cosmiconfig@8.0.0: version "8.0.0" resolved "https://registry.yarnpkg.com/cosmiconfig/-/cosmiconfig-8.0.0.tgz#e9feae014eab580f858f8a0288f38997a7bebe97" @@ -4353,7 +4368,7 @@ npm-run-path@^4.0.1: dependencies: path-key "^3.0.0" -object-assign@^4.1.1: +object-assign@^4, object-assign@^4.1.1: version "4.1.1" resolved "https://registry.npmjs.org/object-assign/-/object-assign-4.1.1.tgz" integrity sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg== @@ -5688,6 +5703,11 @@ v8-to-istanbul@^9.0.1: "@types/istanbul-lib-coverage" "^2.0.1" convert-source-map "^1.6.0" +vary@^1: + version "1.1.2" + resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc" + integrity sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg== + void-elements@3.1.0: version "3.1.0" resolved "https://registry.yarnpkg.com/void-elements/-/void-elements-3.1.0.tgz#614f7fbf8d801f0bb5f0661f5b2f5785750e4f09"