Skip to content

Commit 151b431

Browse files
committed
add cors
1 parent d0ce671 commit 151b431

File tree

5 files changed

+30
-4
lines changed

5 files changed

+30
-4
lines changed

.env.example

+1
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ POSTGRES_PORT=5432
55
POSTGRES_USER=postgres
66

77
JWT_SECRET=secret
8+
FRONTEND_URL=http://localhost:3000

package.json

+2
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
"dependencies": {
1818
"bcrypt": "^5.1.1",
1919
"cookie-parser": "^1.4.7",
20+
"cors": "^2.8.5",
2021
"dotenv": "^16.4.5",
2122
"envalid": "^8.0.0",
2223
"express": "^4.21.1",
@@ -27,6 +28,7 @@
2728
"devDependencies": {
2829
"@types/bcrypt": "^5.0.2",
2930
"@types/cookie-parser": "^1.4.7",
31+
"@types/cors": "^2.8.17",
3032
"@types/express": "^5.0.0",
3133
"@types/jsonwebtoken": "^9.0.7",
3234
"@types/node": "^22.7.7",

src/app.ts

+7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import express from "express";
2+
import cors from "cors";
23
import { env } from "config/env";
34
import errorMiddleware from "middleware/error.middleware";
45
import { type Controller } from "interfaces/controller.interface";
@@ -18,6 +19,12 @@ class App {
1819

1920
private initializeMiddlewares() {
2021
this.app.use(express.json());
22+
this.app.use(
23+
cors({
24+
origin: env.FRONTEND_URL,
25+
credentials: true
26+
})
27+
);
2128
this.app.use(cookieParser());
2229
}
2330

src/config/env.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { config } from "dotenv";
2-
import { cleanEnv, port, str } from "envalid";
2+
import { cleanEnv, port, str, url } from "envalid";
33

44
config();
55

@@ -12,5 +12,6 @@ export const env = cleanEnv(process.env, {
1212

1313
PORT: port({ default: 5000 }),
1414

15-
JWT_SECRET: str()
15+
JWT_SECRET: str(),
16+
FRONTEND_URL: url()
1617
});

yarn.lock

+17-2
Original file line numberDiff line numberDiff line change
@@ -402,6 +402,13 @@
402402
dependencies:
403403
"@types/express" "*"
404404

405+
"@types/cors@^2.8.17":
406+
version "2.8.17"
407+
resolved "https://registry.yarnpkg.com/@types/cors/-/cors-2.8.17.tgz#5d718a5e494a8166f569d986794e49c48b216b2b"
408+
integrity sha512-8CGDvrBj1zgo2qE+oS3pOCyYNqCPryMWY2bGfwA0dcfopWGgxs+78df0Rs3rc9THP4JkOhLsAa+15VdpAqkcUA==
409+
dependencies:
410+
"@types/node" "*"
411+
405412
"@types/[email protected]", "@types/estree@^1.0.6":
406413
version "1.0.6"
407414
resolved "https://registry.yarnpkg.com/@types/estree/-/estree-1.0.6.tgz#628effeeae2064a1b4e79f78e81d87b7e5fc7b50"
@@ -881,6 +888,14 @@ [email protected]:
881888
resolved "https://registry.yarnpkg.com/cookie/-/cookie-0.7.2.tgz#556369c472a2ba910f2979891b526b3436237ed7"
882889
integrity sha512-yki5XnKuf750l50uGTllt6kKILY4nQ1eNIQatoXEByZ5dWgnKqbnqmTrBE5B4N7lrMJKQ2ytWMiTO2o0v6Ew/w==
883890

891+
cors@^2.8.5:
892+
version "2.8.5"
893+
resolved "https://registry.yarnpkg.com/cors/-/cors-2.8.5.tgz#eac11da51592dd86b9f06f6e7ac293b3df875d29"
894+
integrity sha512-KIHbLJqu73RGr/hnbrO9uBeixNGuvSQjul/jdFvS/KFSIH1hWVd1ng7zOHx+YrEfInLG7q4n6GHQ9cDtxv/P6g==
895+
dependencies:
896+
object-assign "^4"
897+
vary "^1"
898+
884899
cross-spawn@^7.0.0, cross-spawn@^7.0.2, cross-spawn@^7.0.3:
885900
version "7.0.3"
886901
resolved "https://registry.yarnpkg.com/cross-spawn/-/cross-spawn-7.0.3.tgz#f73a85b9d5d41d045551c177e2882d4ac85728a6"
@@ -1947,7 +1962,7 @@ npmlog@^5.0.1:
19471962
gauge "^3.0.0"
19481963
set-blocking "^2.0.0"
19491964

1950-
object-assign@^4.0.1, object-assign@^4.1.1:
1965+
object-assign@^4, object-assign@^4.0.1, object-assign@^4.1.1:
19511966
version "4.1.1"
19521967
resolved "https://registry.yarnpkg.com/object-assign/-/object-assign-4.1.1.tgz#2109adc7965887cfc05cbbd442cac8bfbb360863"
19531968
integrity sha512-rJgTQnkUnH1sFw8yT6VSU3zD3sWmu6sZhIseY8VX+GRu3P6F7Fu+JNDoXfklElbLJSnc3FUQHVe4cU5hj+BcUg==
@@ -2739,7 +2754,7 @@ [email protected]:
27392754
resolved "https://registry.yarnpkg.com/utils-merge/-/utils-merge-1.0.1.tgz#9f95710f50a267947b2ccc124741c1028427e713"
27402755
integrity sha512-pMZTvIkT1d+TFGvDOqodOclx0QWkkgi6Tdoa8gC8ffGAAqz9pzPTZWAybbsHHoED/ztMtkv/VoYTYyShUn81hA==
27412756

2742-
vary@~1.1.2:
2757+
vary@^1, vary@~1.1.2:
27432758
version "1.1.2"
27442759
resolved "https://registry.yarnpkg.com/vary/-/vary-1.1.2.tgz#2299f02c6ded30d4a5961b0b9f74524a18f634fc"
27452760
integrity sha512-BNGbWLfd0eUPabhkXUVm0j8uuvREyTh5ovRa/dyow/BqAbZJyC+5fU+IzQOzmAKzYqYRAISoRhdQr3eIZ/PXqg==

0 commit comments

Comments
 (0)