Skip to content

Commit

Permalink
use cors from middleware
Browse files Browse the repository at this point in the history
  • Loading branch information
thanhdanh27600 committed Aug 28, 2023
1 parent 7f771e7 commit a5410de
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 41 deletions.
25 changes: 23 additions & 2 deletions middleware.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@
// middleware.ts
import { NextRequest, NextResponse } from 'next/server';
import { alternateBrandUrl, brandUrl, brandUrlShort } from 'types/constants';

// This function can be marked `async` if using `await` inside
export async function middleware(request: NextRequest) {
return NextResponse.next();
// the list of all allowed origins
const allowedOrigins = [brandUrl, brandUrlShort, ...[alternateBrandUrl]];
// retrieve the current response
const res = NextResponse.next();
// retrieve the HTTP "Origin" header
// from the incoming request
request.headers.get('origin');
// if the origin is an allowed one,
// add it to the 'Access-Control-Allow-Origin' header
if (allowedOrigins.includes(origin)) {
res.headers.append('Access-Control-Allow-Origin', origin);
}
// add the remaining CORS headers to the response
res.headers.append('Access-Control-Allow-Credentials', 'true');
res.headers.append('Access-Control-Allow-Methods', 'GET,DELETE,PATCH,POST,PUT');
res.headers.append(
'Access-Control-Allow-Headers',
'X-CSRF-Token, X-Requested-With, Accept, Accept-Version, Content-Length, Content-MD5, Content-Type, Date, X-Api-Version',
);

return res;
}

// Limit the middleware to paths starting with `/api/`
export const config = {
matcher: '/api/:function*',
matcher: '/api/:path*',
};
2 changes: 0 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"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",
Expand Down Expand Up @@ -60,7 +59,6 @@
"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",
Expand Down
22 changes: 12 additions & 10 deletions src/api/axios.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import axios from 'axios';
import Cors from 'cors';
import { NextApiRequest, NextApiResponse } from 'next';
import { BASE_URL, alternateBrandUrl, brandUrl, brandUrlShort } from '../types/constants';
import { BASE_URL } from '../types/constants';

export const API = axios.create({
baseURL: BASE_URL,
Expand All @@ -23,17 +22,20 @@ export function withAuth(token?: string) {
};
}

const cors = Cors({
origin: [brandUrl, brandUrlShort, ...alternateBrandUrl],
methods: ['GET', 'POST', 'PUT', 'PATCH'],
});

/**
* @deprecated The method should not be used
*/
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;
}
cors(req, res, async () => {
return await handler(req, res);
});
return await handler(req, res);
};
3 changes: 1 addition & 2 deletions src/pages/api/f.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { allowCors } from 'api/axios';
import { f } from 'controllers';
// f = forward
export default allowCors(f.handler);
export default f.handler;
3 changes: 1 addition & 2 deletions src/pages/api/forward.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { allowCors } from 'api/axios';
import { forward } from 'controllers';

export default allowCors(forward.handler);
export default forward.handler;
3 changes: 1 addition & 2 deletions src/pages/api/i.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { allowCors } from 'api/axios';
import { i } from 'controllers';
// i = mage
export default allowCors(i.handler);
export default i.handler;
22 changes: 1 addition & 21 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -1168,13 +1168,6 @@
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"
Expand Down Expand Up @@ -2031,14 +2024,6 @@ 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"
Expand Down Expand Up @@ -4368,7 +4353,7 @@ npm-run-path@^4.0.1:
dependencies:
path-key "^3.0.0"

object-assign@^4, object-assign@^4.1.1:
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==
Expand Down Expand Up @@ -5703,11 +5688,6 @@ 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"
Expand Down

0 comments on commit a5410de

Please sign in to comment.