Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

App with all functionalities completely working #22

Open
wants to merge 14 commits into
base: main
Choose a base branch
from
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@

## Build a basic version of PayTM
## Build a basic version of Easy Pay App
3 changes: 3 additions & 0 deletions backend/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
module.exports = {
JWT_SECRET: "I_KNOW_YOUR_SECRET",
};
82 changes: 82 additions & 0 deletions backend/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
const mongoose = require("mongoose");
const bcrypt = require("bcrypt");

main().catch((err) => console.log(err));

async function main() {
await mongoose.connect(
"mongodb+srv://dtsri:[email protected]/users"
);
}

//creating a schema
const UserSchema = new mongoose.Schema({
username: {
type: String,
required: true,
unique: true,
trim: true,
lowercase: true,
minLength: 3,
maxLength: 30,
},
password: {
type: String,
required: true,
minLength: 6,
},
firstName: {
type: String,
required: true,
trim: true,
maxLength: 25,
},
lastName: {
type: String,
required: true,
trim: true,
maxLength: 25,
},
});

// Method to generate a hash from plain text
UserSchema.methods.createHash = async function (plainTextPassword) {
// Hashing user's salt and password with 10 iterations,
const saltRounds = 10;

// First method to generate a salt and then create hash
const salt = await bcrypt.genSalt(saltRounds);

//generates the hashPassword
return await bcrypt.hash(plainTextPassword, salt);
};

//For validation, get the password from DB (this.password --> hashPassword) and compare it to the input Password provided by the client
//note: we dont implement this method
UserSchema.methods.validatePassword = async function (candidatePassword) {
//returns a boolean indicating if passwords matches or not
return await bcrypt.compare(candidatePassword, this.password);
};

//creating a User Model aka creating a class
const User = mongoose.model("USER", UserSchema);

const AccountSchema = new mongoose.Schema({
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: "User", //this refferring to the document of the User Model
//for value we have to just metnion ref:user._id
required: true,
},
balance: {
type: Number,
required: true,
},
});

const Account = mongoose.model("Account", AccountSchema);

module.exports = {
User,
Account,
};
12 changes: 12 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
const express = require("express");
const cors = require("cors");
const rootRouter = require("./routes/index");

const app = express();

app.use(cors()); //enabling the cross origing resoure sharing policy since frontend and backend are two differnt origins,
//frontend will request from backend.

app.use(express.json()); //parses the JSON //request body-parser
app.use("/api/v1", rootRouter);

app.listen(3000, () => {
console.log(`server is up and running`);
});
37 changes: 37 additions & 0 deletions backend/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const jwt = require("jsonwebtoken");
const { JWT_SECRET } = require("./config");

//this function is mainly to indentify the person via jsonwebtoken

async function authMiddleware(req, res, next) {
const authHeader = req.headers?.authorization;

console.log(authHeader);

if (!authHeader || !authHeader.startsWith("Bearer")) {
res.status(403).json({
message: "There is an issue with authorization headers",
});
}

const token = authHeader.split(" ")[1];
jwt.verify(token, JWT_SECRET, (err, payload) => {
if (err) {
res.status(403).json({
message: "Please check the login credentials",
});
} else {
const userId = payload.userId;

//putting the userID in the request body, so that it can be used by next callback function
//in a route, you can add multiple callback functions
req.userId = userId;

next();
}
});
}

module.exports = {
authMiddleware,
};
Loading