Skip to content
This repository was archived by the owner on Dec 22, 2023. It is now read-only.

Commit 7abc044

Browse files
full server
1 parent 60a36c5 commit 7abc044

File tree

16 files changed

+1411
-0
lines changed

16 files changed

+1411
-0
lines changed

coinstrikeServer/.gitignore

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
# Logs
2+
logs
3+
*.log
4+
npm-debug.log*
5+
yarn-debug.log*
6+
yarn-error.log*
7+
pnpm-debug.log*
8+
lerna-debug.log*
9+
10+
node_modules
11+
dist
12+
dist-ssr
13+
*.local
14+
15+
# Editor directories and files
16+
.vscode/*
17+
!.vscode/extensions.json
18+
.idea
19+
.DS_Store
20+
*.suo
21+
*.ntvs*
22+
*.njsproj
23+
*.sln
24+
*.sw?
25+
*.env
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
const jwt = require("jsonwebtoken");
2+
const asyncHandler = require("express-async-handler");
3+
const User = require("../Models/userModel.js");
4+
5+
const protect = asyncHandler(async (req, res, next) => {
6+
let token;
7+
8+
if (
9+
req.headers.authorization &&
10+
req.headers.authorization.startsWith("Bearer")
11+
) {
12+
try {
13+
token = req.headers.authorization.split(" ")[1];
14+
15+
const decoded = jwt.verify(token, process.env.JWT_SECRET);
16+
17+
req.user = await User.findById(decoded.id).select("-password");
18+
next();
19+
} catch (error) {
20+
console.error(error);
21+
res.status(401);
22+
throw new Error("Not authorized, token failed");
23+
}
24+
}
25+
if (!token) {
26+
res.status(401);
27+
throw new Error("Not authorized, no token");
28+
}
29+
});
30+
31+
const admin = (req, res, next) => {
32+
if (req.user && req.user.isAdmin) {
33+
next();
34+
} else {
35+
res.status(401);
36+
throw new Error("Not authorized as an Admin");
37+
}
38+
};
39+
40+
const vendor = (req, res, next) => {
41+
if (req.user && req.user.isVendor) {
42+
next();
43+
} else {
44+
res.status(401);
45+
throw new Error("Not authorized as an Vendor");
46+
}
47+
};
48+
49+
module.exports = { protect, admin, vendor };

coinstrikeServer/Middleware/Errors.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
const notFound = (req, res, next) => {
2+
const error = new Error(`Not found - ${req.originalUrl}`);
3+
res.status(404);
4+
next(error);
5+
};
6+
7+
const errorHandler = (err, req, res, next) => {
8+
const statusCode = res.statusCode === 200 ? 500 : res.statusCode;
9+
res.status(statusCode);
10+
res.json({
11+
message: err.message,
12+
stack: process.env.NODE_ENV === "production" ? null : err.stack,
13+
});
14+
};
15+
16+
module.exports = { notFound, errorHandler };
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
const mongoose = require("mongoose");
2+
3+
// Creating the schema for the transaction model fields of user id, name, description, amount, category, type, cycle, status, auto, and date
4+
const transactionSchema = mongoose.Schema(
5+
{
6+
user: {
7+
type: mongoose.Schema.ObjectId,
8+
required: true,
9+
ref: "User",
10+
},
11+
name: {
12+
type: String,
13+
required: true,
14+
},
15+
description: {
16+
type: String,
17+
},
18+
amount: {
19+
type: Number,
20+
required: true,
21+
},
22+
category: {
23+
type: String,
24+
},
25+
type: {
26+
type: String,
27+
required: true,
28+
enum: ["income", "expense", "subscription"],
29+
},
30+
cycle: {
31+
type: String,
32+
enum: ["monthly", "quarterly", "annual"],
33+
},
34+
status: {
35+
type: Boolean,
36+
},
37+
auto: {
38+
type: Boolean,
39+
},
40+
date: {
41+
type: Date,
42+
required: true,
43+
set: function (value) {
44+
// Extracting the date portion
45+
const [year, month, day, hour, minute, second] = value.split(":");
46+
const newDate = new Date(year, month, day, hour, minute, second);
47+
48+
return newDate;
49+
},
50+
},
51+
},
52+
{
53+
timestamps: true,
54+
}
55+
);
56+
57+
const Transaction = mongoose.model("Entry", transactionSchema);
58+
59+
module.exports = Transaction;

coinstrikeServer/Models/userModel.js

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
const mongoose = require("mongoose");
2+
const bcrypt = require("bcryptjs");
3+
4+
const userSchema = new mongoose.Schema(
5+
{
6+
name: {
7+
type: String,
8+
},
9+
lastname: {
10+
type: String,
11+
},
12+
date: {
13+
type: Date,
14+
},
15+
salary: {
16+
type: Number,
17+
},
18+
email: {
19+
type: String,
20+
required: true,
21+
unique: true,
22+
},
23+
image: {
24+
type: String,
25+
},
26+
password: {
27+
type: String,
28+
required: true,
29+
},
30+
currency: {
31+
type: String,
32+
default: "INR",
33+
},
34+
expense: {
35+
type: Number,
36+
default: 0,
37+
},
38+
expenseList: {
39+
type: Array,
40+
default: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
41+
},
42+
income: {
43+
type: Number,
44+
default: 0,
45+
},
46+
incomeList: {
47+
type: Array,
48+
default: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
49+
},
50+
otp: {
51+
type: String,
52+
default: "",
53+
},
54+
isVerified: {
55+
type: Boolean,
56+
default: false,
57+
},
58+
},
59+
{
60+
timestamps: true,
61+
}
62+
);
63+
64+
// Login
65+
userSchema.methods.matchPassword = async function (enterPassword) {
66+
return await bcrypt.compare(enterPassword, this.password);
67+
};
68+
69+
// Login
70+
userSchema.methods.matchOtp = async function (otpEnter) {
71+
return await bcrypt.compare(otpEnter, this.otp);
72+
};
73+
74+
// Register
75+
userSchema.pre("save", async function (next) {
76+
if (!this.isModified("password")) {
77+
next();
78+
}
79+
const salt = await bcrypt.genSalt(10);
80+
this.password = await bcrypt.hash(this.password, salt);
81+
});
82+
83+
const User = mongoose.model("User", userSchema);
84+
85+
module.exports = User;

coinstrikeServer/dbConfig.js

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const mongoose = require("mongoose");
2+
3+
const cluster = "clusterdb.ok95nts";
4+
const dbname = "coin";
5+
6+
const url = `mongodb+srv://${process.env.db_USERNAME}:${process.env.db_PASSWORD}@${cluster}.mongodb.net/${dbname}?retryWrites=true&w=majority`;
7+
8+
async function connectDB() {
9+
try {
10+
await mongoose.connect(url, {
11+
useNewUrlParser: true,
12+
useUnifiedTopology: true,
13+
});
14+
console.log("Connected to MongoDB");
15+
16+
//verification
17+
const db = mongoose.connection;
18+
db.on("error", console.error.bind(console, "connection error: "));
19+
db.once("open", function () {
20+
console.log("Connected successfully");
21+
});
22+
} catch (error) {
23+
console.error(error);
24+
}
25+
}
26+
27+
module.exports = connectDB;

coinstrikeServer/lib/PaytmChecksum.js

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
"use strict";
2+
3+
var crypto = require("crypto");
4+
5+
class PaytmChecksum {
6+
static encrypt(input, key) {
7+
var cipher = crypto.createCipheriv("AES-128-CBC", key, PaytmChecksum.iv);
8+
var encrypted = cipher.update(input, "binary", "base64");
9+
encrypted += cipher.final("base64");
10+
return encrypted;
11+
}
12+
static decrypt(encrypted, key) {
13+
var decipher = crypto.createDecipheriv(
14+
"AES-128-CBC",
15+
key,
16+
PaytmChecksum.iv
17+
);
18+
var decrypted = decipher.update(encrypted, "base64", "binary");
19+
try {
20+
decrypted += decipher.final("binary");
21+
} catch (e) {
22+
console.log(e);
23+
}
24+
return decrypted;
25+
}
26+
static generateSignature(params, key) {
27+
if (typeof params !== "object" && typeof params !== "string") {
28+
var error = "string or object expected, " + typeof params + " given.";
29+
return Promise.reject(error);
30+
}
31+
if (typeof params !== "string") {
32+
params = PaytmChecksum.getStringByParams(params);
33+
}
34+
return PaytmChecksum.generateSignatureByString(params, key);
35+
}
36+
37+
static verifySignature(params, key, checksum) {
38+
if (typeof params !== "object" && typeof params !== "string") {
39+
var error = "string or object expected, " + typeof params + " given.";
40+
return Promise.reject(error);
41+
}
42+
if (params.hasOwnProperty("CHECKSUMHASH")) {
43+
delete params.CHECKSUMHASH;
44+
}
45+
if (typeof params !== "string") {
46+
params = PaytmChecksum.getStringByParams(params);
47+
}
48+
return PaytmChecksum.verifySignatureByString(params, key, checksum);
49+
}
50+
51+
static async generateSignatureByString(params, key) {
52+
var salt = await PaytmChecksum.generateRandomString(4);
53+
return PaytmChecksum.calculateChecksum(params, key, salt);
54+
}
55+
56+
static verifySignatureByString(params, key, checksum) {
57+
var paytm_hash = PaytmChecksum.decrypt(checksum, key);
58+
var salt = paytm_hash.substr(paytm_hash.length - 4);
59+
return paytm_hash === PaytmChecksum.calculateHash(params, salt);
60+
}
61+
62+
static generateRandomString(length) {
63+
return new Promise(function (resolve, reject) {
64+
crypto.randomBytes((length * 3.0) / 4.0, function (err, buf) {
65+
if (!err) {
66+
var salt = buf.toString("base64");
67+
resolve(salt);
68+
} else {
69+
console.log("error occurred in generateRandomString: " + err);
70+
reject(err);
71+
}
72+
});
73+
});
74+
}
75+
76+
static getStringByParams(params) {
77+
var data = {};
78+
Object.keys(params)
79+
.sort()
80+
.forEach(function (key, value) {
81+
data[key] =
82+
params[key] !== null && params[key].toLowerCase() !== "null"
83+
? params[key]
84+
: "";
85+
});
86+
return Object.values(data).join("|");
87+
}
88+
89+
static calculateHash(params, salt) {
90+
var finalString = params + "|" + salt;
91+
return crypto.createHash("sha256").update(finalString).digest("hex") + salt;
92+
}
93+
static calculateChecksum(params, key, salt) {
94+
var hashString = PaytmChecksum.calculateHash(params, salt);
95+
return PaytmChecksum.encrypt(hashString, key);
96+
}
97+
}
98+
PaytmChecksum.iv = "@@@@&&&&####$$$$";
99+
module.exports = PaytmChecksum;

coinstrikeServer/package.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"name": "coinstrike",
3+
"version": "1.0.0",
4+
"description": "",
5+
"main": "server.js",
6+
"scripts": {
7+
"test": "echo \"Error: no test specified\" && exit 1"
8+
},
9+
"author": "",
10+
"license": "ISC"
11+
}

0 commit comments

Comments
 (0)