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

Added the auth header verification to fetch the users #24

Open
wants to merge 22 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 Payments App
1 change: 1 addition & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.env
6 changes: 6 additions & 0 deletions backend/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
const dotenv = require('dotenv')
dotenv.config();

module.exports = {
JWT_SECRET : process.env.JWT_SECRET
};
28 changes: 28 additions & 0 deletions backend/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
const mongoose = require("mongoose");
const dotenv = require('dotenv')
dotenv.config();

mongoose.connect(process.env.MONGO_URL)

const userSchema = new mongoose.Schema({
username: String,
password: String,
firstName: String,
lastName: String
})
const AccountSchema = new mongoose.Schema({
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: "User"
},
balance: Number
})


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

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

app.use(cors());
app.use(express.json());

const mainRouter = require("./routes/index")


app.use("/api/v1/", mainRouter);

app.listen(3000);

26 changes: 26 additions & 0 deletions backend/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
const { JWT_SECRET } = require("./config");
const jwt = require("jsonwebtoken");

const authMiddleware = (req, res, next) => {
const authHeader = req.headers.authorization;

if (!authHeader || !authHeader.startsWith('Bearer ')) {
return res.status(403).json({});
}

const token = authHeader.split(' ')[1];

try {
const decoded = jwt.verify(token, JWT_SECRET);

req.userId = decoded.userId;

next();
} catch (err) {
return res.status(403).json({});
}
};

module.exports = {
authMiddleware
}
157 changes: 157 additions & 0 deletions backend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@
"author": "",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"dotenv": "^16.4.5",
"express": "^4.18.2",
"jsonwebtoken": "^9.0.2",
"mongoose": "^8.1.0",
"zod": "^3.22.4"
}
Expand Down
55 changes: 55 additions & 0 deletions backend/routes/account.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
const express = require("express");
const { authMiddleware } = require("../middleware");
const { Account } = require("../db");
const mongoose = require("mongoose");

const router = express.Router();

router.get("/balance", authMiddleware, async(req,res) => {
const account = await Account.findOne({
userId : req.userId

});

res.json({
balance : account.balance
})
});

router.post("/transfer", authMiddleware, async (req,res) =>{
// async function transfer(req) {

const session = await mongoose.startSession();


session.startTransaction();
const { amount, to } = req.body;

const account = await Account.findOne({userId : req.userId}).session(session);

if(!account || account.balance < amount){
await session.abortTransaction();
return res.status(400).json({
message : "Insufficient balance"
});
}

const toAccount = await Account.findOne({userId: to}).session(session);

if(!account){
await session.abortTransaction();
return res.status(400).json({
message: " Invalid account"
});
}

await Account.updateOne({ userId : req.userId }, {$inc : {balance : -amount} }).session(session);
await Account.updateOne({ userId : to}, {$inc : {balance : amount} }).session(session);

await session.commitTransaction();
res.json({
message: "Transfer successful"
});
});

module.exports = router;
10 changes: 10 additions & 0 deletions backend/routes/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
const express = require("express")
const userRouter = require("./user")
const accountRouter = require("./account");

const router = express.Router();

router.use("/user",userRouter)
router.use("/account",accountRouter)

module.exports = router;
Loading