Skip to content

Commit

Permalink
created a seperate environment for development and production
Browse files Browse the repository at this point in the history
  • Loading branch information
victorchrollo14 committed Jan 22, 2024
1 parent 295689c commit c38089e
Show file tree
Hide file tree
Showing 9 changed files with 48 additions and 18 deletions.
4 changes: 2 additions & 2 deletions client/src/pages/authentication/LoginPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ const LoginPage = () => {
<Link to={"/register"}>Register</Link>
</span>
</div>
<GithubButton
{/* <GithubButton
name={"Login with Github"}
setError={setError}
setSuccess={setSuccess}
/>
/> */}
<GoogleButton>Login With Google</GoogleButton>
<div className="text-center text-2xl font-semibold my-6 flex">
{" "}
Expand Down
4 changes: 3 additions & 1 deletion server/.env.example
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
MONGODB_URI=mongodb://127.0.0.1:27017/blackmarket
JWT_SECRET=jwt_secret
JWT_SECRET=jwt_secret
CLIENT_ID=github_client_id
CLIENT_SECRET=github_secret
25 changes: 25 additions & 0 deletions server/config.js
Original file line number Diff line number Diff line change
@@ -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 };
12 changes: 5 additions & 7 deletions server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -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"));
Expand All @@ -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}`);
Expand Down
3 changes: 2 additions & 1 deletion server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
9 changes: 5 additions & 4 deletions server/src/controller/oauthController.js
Original file line number Diff line number Diff line change
@@ -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", {
Expand All @@ -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}`;
Expand Down Expand Up @@ -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",
});

Expand All @@ -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",
});

Expand Down
3 changes: 2 additions & 1 deletion server/src/middleware/auth.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import jwt from "jsonwebtoken";
import { JWT_PRIVATE_KEY } from "../../config.js";

const verifyToken = async (req, res, next) => {
try {
const token = req.header("Authorization");

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) {
Expand Down
3 changes: 2 additions & 1 deletion server/src/utils/insertData.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion server/tests/testIndex.js
Original file line number Diff line number Diff line change
@@ -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 () => {
Expand Down

0 comments on commit c38089e

Please sign in to comment.