Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .env.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
PORT =
DB_CONNECTION_SECRET =
JWT_SECRET =

RAZORPAY_KEY_ID =
RAZORPAY_KEY_SECRET =
RAZORPAY_WEBHOOK_SECRET =

AWS_ACCESS_KEY =
AWS_SECRET_KEY =
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
},
"keywords": [
"nodejs",
"mongodb",
"express",
"javascript"
],
"author": "Akshay Saini",
Expand Down
9 changes: 4 additions & 5 deletions src/app.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
const express = require("express");
const connectDB = require("./config/database");
const app = express();
const cookieParser = require("cookie-parser");
const cors = require("cors");
const http = require("http");

require("dotenv").config();
const { PORT } = require("./config/config");

require("./utils/cronjob");

const app = express();
app.use(
cors({
origin: "http://localhost:5173",
Expand All @@ -23,8 +22,8 @@ const profileRouter = require("./routes/profile");
const requestRouter = require("./routes/request");
const userRouter = require("./routes/user");
const paymentRouter = require("./routes/payment");
const initializeSocket = require("./utils/socket");
const chatRouter = require("./routes/chat");
const initializeSocket = require("./utils/socket");

app.use("/", authRouter);
app.use("/", profileRouter);
Expand All @@ -39,7 +38,7 @@ initializeSocket(server);
connectDB()
.then(() => {
console.log("Database connection established...");
server.listen(process.env.PORT, () => {
server.listen(PORT, () => {
console.log("Server is successfully listening on port 7777...");
});
})
Expand Down
14 changes: 14 additions & 0 deletions src/config/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require("dotenv").config();

module.exports = {
PORT: process.env.PORT,
DB_CONNECTION_SECRET: process.env.DB_CONNECTION_SECRET,
JWT_SECRET: process.env.JWT_SECRET,

RAZORPAY_KEY_ID: process.env.RAZORPAY_KEY_ID,
RAZORPAY_KEY_SECRET: process.env.RAZORPAY_KEY_SECRET,
RAZORPAY_WEBHOOK_SECRET: process.env.RAZORPAY_WEBHOOK_SECRET,

AWS_ACCESS_KEY: process.env.AWS_ACCESS_KEY,
AWS_SECRET_KEY: process.env.AWS_SECRET_KEY,
};
4 changes: 2 additions & 2 deletions src/config/database.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
const mongoose = require("mongoose");
const { DB_CONNECTION_SECRET } = require("./config");

const connectDB = async () => {
console.log(process.env.DB_CONNECTION_SECRET);
await mongoose.connect(process.env.DB_CONNECTION_SECRET);
await mongoose.connect(DB_CONNECTION_SECRET);
};

module.exports = connectDB;
7 changes: 3 additions & 4 deletions src/middlewares/auth.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
const jwt = require("jsonwebtoken");
const User = require("../models/user");
const { JWT_SECRET } = require("../config/config");

const userAuth = async (req, res, next) => {
try {
Expand All @@ -8,7 +9,7 @@ const userAuth = async (req, res, next) => {
return res.status(401).send("Please Login!");
}

const decodedObj = await jwt.verify(token, process.env.JWT_SECRET);
const decodedObj = await jwt.verify(token, JWT_SECRET);

const { _id } = decodedObj;

Expand All @@ -24,6 +25,4 @@ const userAuth = async (req, res, next) => {
}
};

module.exports = {
userAuth,
};
module.exports = { userAuth };
3 changes: 2 additions & 1 deletion src/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const mongoose = require("mongoose");
const validator = require("validator");
const jwt = require("jsonwebtoken");
const bcrypt = require("bcrypt");
const { JWT_SECRET } = require("../config/config");

const userSchema = new mongoose.Schema(
{
Expand Down Expand Up @@ -83,7 +84,7 @@ const userSchema = new mongoose.Schema(
userSchema.methods.getJWT = async function () {
const user = this;

const token = await jwt.sign({ _id: user._id }, "DEV@Tinder$790", {
const token = await jwt.sign({ _id: user._id }, JWT_SECRET, {
expiresIn: "7d",
});

Expand Down
5 changes: 3 additions & 2 deletions src/routes/payment.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const { membershipAmount } = require("../utils/constants");
const {
validateWebhookSignature,
} = require("razorpay/dist/utils/razorpay-utils");
const { RAZORPAY_KEY_ID, RAZORPAY_WEBHOOK_SECRET } = require("../config/config");

paymentRouter.post("/payment/create", userAuth, async (req, res) => {
try {
Expand Down Expand Up @@ -42,7 +43,7 @@ paymentRouter.post("/payment/create", userAuth, async (req, res) => {
const savedPayment = await payment.save();

// Return back my order details to frontend
res.json({ ...savedPayment.toJSON(), keyId: process.env.RAZORPAY_KEY_ID });
res.json({ ...savedPayment.toJSON(), keyId: RAZORPAY_KEY_ID });
} catch (err) {
return res.status(500).json({ msg: err.message });
}
Expand All @@ -57,7 +58,7 @@ paymentRouter.post("/payment/webhook", async (req, res) => {
const isWebhookValid = validateWebhookSignature(
JSON.stringify(req.body),
webhookSignature,
process.env.RAZORPAY_WEBHOOK_SECRET
RAZORPAY_WEBHOOK_SECRET
);

if (!isWebhookValid) {
Expand Down
5 changes: 3 additions & 2 deletions src/utils/razorpay.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
const Razorpay = require("razorpay");
const { RAZORPAY_KEY_ID, RAZORPAY_KEY_SECRET } = require("../config/config");

var instance = new Razorpay({
key_id: process.env.RAZORPAY_KEY_ID,
key_secret: process.env.RAZORPAY_KEY_SECRET,
key_id: RAZORPAY_KEY_ID,
key_secret: RAZORPAY_KEY_SECRET,
});

module.exports = instance;
5 changes: 3 additions & 2 deletions src/utils/sesClient.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
const { SESClient } = require("@aws-sdk/client-ses");
const { AWS_ACCESS_KEY, AWS_SECRET_KEY } = require("../config/config");
// Set the AWS Region.
const REGION = "ap-south-1";
// Create SES service object.
const sesClient = new SESClient({
region: REGION,
credentials: {
accessKeyId: process.env.AWS_ACCESS_KEY,
secretAccessKey: process.env.AWS_SECRET_KEY,
accessKeyId: AWS_ACCESS_KEY,
secretAccessKey: AWS_SECRET_KEY,
},
});

Expand Down