From c38089ea0cc217667f15914385b13e16511d66fa Mon Sep 17 00:00:00 2001 From: victor Date: Mon, 22 Jan 2024 17:20:25 +0530 Subject: [PATCH] created a seperate environment for development and production --- client/src/pages/authentication/LoginPage.jsx | 4 +-- server/.env.example | 4 ++- server/config.js | 25 +++++++++++++++++++ server/index.js | 12 ++++----- server/package.json | 3 ++- server/src/controller/oauthController.js | 9 ++++--- server/src/middleware/auth.js | 3 ++- server/src/utils/insertData.js | 3 ++- server/tests/testIndex.js | 3 ++- 9 files changed, 48 insertions(+), 18 deletions(-) create mode 100644 server/config.js diff --git a/client/src/pages/authentication/LoginPage.jsx b/client/src/pages/authentication/LoginPage.jsx index 277f533..270ff2b 100644 --- a/client/src/pages/authentication/LoginPage.jsx +++ b/client/src/pages/authentication/LoginPage.jsx @@ -28,11 +28,11 @@ const LoginPage = () => { Register - + /> */} Login With Google
{" "} diff --git a/server/.env.example b/server/.env.example index 958a4f3..9c34742 100644 --- a/server/.env.example +++ b/server/.env.example @@ -1,2 +1,4 @@ MONGODB_URI=mongodb://127.0.0.1:27017/blackmarket -JWT_SECRET=jwt_secret \ No newline at end of file +JWT_SECRET=jwt_secret +CLIENT_ID=github_client_id +CLIENT_SECRET=github_secret \ No newline at end of file diff --git a/server/config.js b/server/config.js new file mode 100644 index 0000000..8bc1ce5 --- /dev/null +++ b/server/config.js @@ -0,0 +1,25 @@ +import "dotenv/config"; + +const NODE_ENV = process.env.NODE_ENV; +let corsUrls = []; +const MONGODB_URI = process.env.MONGODB_URI; +const JWT_PRIVATE_KEY = process.env.JWT_PRIVATE_KEY; +const CLIENT_ID = process.env.CLIENT_ID; +const CLIENT_SECRET = process.env.CLIENT_SECRET; + +console.log(MONGODB_URI, JWT_PRIVATE_KEY, CLIENT_ID, CLIENT_SECRET); + +if (NODE_ENV === "production") { + const CLIENT_URL1 = process.env.CLIENT_URL1; + const CLIENT_URL2 = process.env.CLIENT_URL2; + + corsUrls.push(CLIENT_URL1); + corsUrls.push(CLIENT_URL2); +} else if (NODE_ENV === "development") { + const local_client = "http://localhost:3000"; + corsUrls.push(local_client); +} + +console.log(corsUrls); + +export { MONGODB_URI, JWT_PRIVATE_KEY, CLIENT_ID, CLIENT_SECRET, corsUrls }; diff --git a/server/index.js b/server/index.js index efc6aec..014089b 100644 --- a/server/index.js +++ b/server/index.js @@ -4,19 +4,17 @@ import cors from "cors"; import mongoose from "mongoose"; import bodyParser from "body-parser"; import cookieParser from "cookie-parser"; +import { MONGODB_URI, corsUrls, JWT_PRIVATE_KEY } from "./config.js"; import { userRouter } from "./src/routes/userRoute.js"; import { productRouter } from "./src/routes/productRoute.js"; import { cartRouter } from "./src/routes/cartRoute.js"; const app = express(); -const PORT = process.env.PORT || 3001; +const PORT = 3001; const corsOptions = { - origin: [ - "https://mern-ecommerce-frontend-theta.vercel.app", - "https://mern-ecommerce-frontend-git-main-victorchrollo14.vercel.app", - ], + origin: corsUrls, credentials: true, optionSuccessStatus: 200, }; @@ -27,7 +25,7 @@ app.use(express.json()); // parse data from client app.use(bodyParser.urlencoded({ extended: true })); app.use(bodyParser.json()); -app.use(cookieParser(process.env.JWT_PRIVATE_KEY)); +app.use(cookieParser(JWT_PRIVATE_KEY)); // static files fetching app.use("/product/ProductAssets", express.static("ProductAssets")); @@ -43,7 +41,7 @@ app.use("/cart", cartRouter); const runServer = async () => { try { - await mongoose.connect(process.env.MONGODB_URI); + await mongoose.connect(MONGODB_URI); console.log("connected to mongodb"); app.listen(PORT, () => { console.log(`Server started on ${PORT}`); diff --git a/server/package.json b/server/package.json index ce64bb2..bfef838 100644 --- a/server/package.json +++ b/server/package.json @@ -5,7 +5,8 @@ "main": "index.js", "scripts": { "test": "jest", - "start": "node index.js" + "dev": "NODE_ENV=development node --watch index.js", + "prod": "NODE_ENV=production node index.js" }, "author": "", "license": "ISC", diff --git a/server/src/controller/oauthController.js b/server/src/controller/oauthController.js index 44f0b8f..97a5104 100644 --- a/server/src/controller/oauthController.js +++ b/server/src/controller/oauthController.js @@ -1,6 +1,7 @@ import User from "../models/userModel.js"; import "dotenv/config.js"; import Jwt from "jsonwebtoken"; +import { CLIENT_ID, CLIENT_SECRET, JWT_PRIVATE_KEY } from "../../config.js"; const getGithubData = async (access_token) => { const response = await fetch("https://api.github.com/user", { @@ -14,8 +15,8 @@ const getGithubData = async (access_token) => { }; const getAccessToken = async (req, res) => { - const CLIENT_ID = process.env.CLIENT_ID; - const CLIENT_SECRET = process.env.CLIENT_SECRET; + const CLIENT_ID = CLIENT_ID; + const CLIENT_SECRET = CLIENT_SECRET; const code = req.query.code; const params = `?client_id=${CLIENT_ID}&client_secret=${CLIENT_SECRET}&code=${code}`; @@ -67,7 +68,7 @@ const githubAuth = async (req, res) => { const user = await User.findOne(query); if (user) { - const token = Jwt.sign({ id: user._id }, process.env.JWT_PRIVATE_KEY, { + const token = Jwt.sign({ id: user._id }, JWT_PRIVATE_KEY, { expiresIn: "30d", }); @@ -88,7 +89,7 @@ const githubAuth = async (req, res) => { await newUser.save(); - const token = Jwt.sign({ id: newUser._id }, process.env.JWT_PRIVATE_KEY, { + const token = Jwt.sign({ id: newUser._id }, JWT_PRIVATE_KEY, { expiresIn: "30d", }); diff --git a/server/src/middleware/auth.js b/server/src/middleware/auth.js index 94324f6..b3fe265 100644 --- a/server/src/middleware/auth.js +++ b/server/src/middleware/auth.js @@ -1,4 +1,5 @@ import jwt from "jsonwebtoken"; +import { JWT_PRIVATE_KEY } from "../../config.js"; const verifyToken = async (req, res, next) => { try { @@ -6,7 +7,7 @@ const verifyToken = async (req, res, next) => { if (!token) return res.status(401).send("access denied"); - const verifiedUser = jwt.verify(token, process.env.JWT_PRIVATE_KEY); + const verifiedUser = jwt.verify(token, JWT_PRIVATE_KEY); req.user = verifiedUser; next(); } catch (err) { diff --git a/server/src/utils/insertData.js b/server/src/utils/insertData.js index d3f1551..5cb2e62 100644 --- a/server/src/utils/insertData.js +++ b/server/src/utils/insertData.js @@ -3,6 +3,7 @@ import dotenv from "dotenv"; import { fileURLToPath } from "url"; import path from "path"; import fs from "fs/promises"; +import { MONGODB_URI } from "../../config.js"; import Product from "../models/productModel.js"; @@ -30,7 +31,7 @@ const insertProducts = async () => { const connect = async () => { try { - await mongoose.connect(process.env.MONGODB_URI); + await mongoose.connect(MONGODB_URI); console.log("connected to mongodb"); // inserting data into db. diff --git a/server/tests/testIndex.js b/server/tests/testIndex.js index 9df3cd0..72ef4c4 100644 --- a/server/tests/testIndex.js +++ b/server/tests/testIndex.js @@ -1,12 +1,13 @@ import express from "express"; import mongoose from "mongoose"; import { MongoMemoryServer } from "mongodb-memory-server"; +import { MONGODB_URI } from "../config.js"; const testApp = express(); testApp.use(express.json()); const connectDB = async () => { - await mongoose.connect(process.env.MONGODB_URI); + await mongoose.connect(MONGODB_URI); }; const disconnectDB = async () => {