Skip to content

Commit

Permalink
fix cors
Browse files Browse the repository at this point in the history
  • Loading branch information
dolphm committed Dec 26, 2024
1 parent b8be7ca commit 4b648e6
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 5 deletions.
4 changes: 2 additions & 2 deletions src/controllers/upload/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { NextApiRequest, NextApiResponse } from 'next/types';
import path from 'path';
import requestIp from 'request-ip';
import prisma from '../../services/db/prisma';
import { isDebug, LIMIT_FILE_UPLOAD } from '../../types/constants';
import { isLocal, LIMIT_FILE_UPLOAD } from '../../types/constants';
import { api, successHandler } from '../../utils/axios';
import HttpStatusCode from '../../utils/statusCode';
import { generateFileName } from '../../utils/text';
Expand Down Expand Up @@ -66,7 +66,7 @@ export const handler = api<any>(
});
return successHandler(res, {
filename: uniqueFileName,
blob: isDebug ? blob : undefined,
blob: isLocal ? blob : undefined,
});
} catch (error: any) {
return res.status(HttpStatusCode.BAD_GATEWAY).json({ error: 'File upload failed ' + error?.message });
Expand Down
3 changes: 2 additions & 1 deletion src/pages/api/upload/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { upload } from 'controllers';
import { cors } from '../../../requests/api';

export default upload.handler;
export default cors(upload.handler);

export const config = {
api: {
Expand Down
19 changes: 17 additions & 2 deletions src/requests/api.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import axios from 'axios';
import { NextApiRequest, NextApiResponse } from 'next';
import { BASE_URL, brandUrl, brandUrlShort, localUrlShort } from '../types/constants';
import { BASE_URL, brandUrl, brandUrlShort, isLocal, localUrl, localUrlShort } from '../types/constants';
import HttpStatusCode from '../utils/statusCode';

export const API = axios.create({
baseURL: BASE_URL,
Expand All @@ -26,7 +27,7 @@ export function withAuth(token?: string) {
};
}

const allowedOrigins = [brandUrl, localUrlShort, brandUrlShort];
const allowedOrigins = isLocal ? [localUrl, localUrlShort] : [brandUrl, brandUrlShort];

export const allowCors = (handler: any) => async (req: NextApiRequest, res: NextApiResponse) => {
const origin = req.headers?.origin;
Expand All @@ -46,3 +47,17 @@ export const allowCors = (handler: any) => async (req: NextApiRequest, res: Next
}
return await handler(req, res);
};

export const cors = (handler: any) => async (req: NextApiRequest, res: NextApiResponse) => {
const origin = req.headers?.origin;
console.log('origin', origin);

// Allow requests from the same origin
if (!!origin && allowedOrigins.includes(origin)) {
return await handler(req, res);
}

// Block requests from other origins
res.setHeader('Access-Control-Allow-Origin', 'false');
return res.status(HttpStatusCode.FORBIDDEN).send('CORS policy: No access from this origin');
};

0 comments on commit 4b648e6

Please sign in to comment.