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 sign out feature and actual balance feature #46

Open
wants to merge 15 commits into
base: complete-solution
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
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
FROM mongo:4.4.7
RUN echo "rs.initiate();" > /docker-entrypoint-initdb.d/replica-init.js
CMD [ "--replSet", "rs" ]
3 changes: 3 additions & 0 deletions backend/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
const JWT_SECRET="mysecret";

module.exports=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');

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

async function main() {
await mongoose.connect('mongodb://localhost:27017/paytm');
console.log("connected to db");
}

const userSchema=mongoose.Schema({
firstName:String,
lastName:String,
password:String,
username:String
});

const accountSchema=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
}
9 changes: 9 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
const express = require("express");
const mainRouter = require("./routes/index");
const app=express();
const cors=require("cors");
app.use(cors());
app.use(express.json());

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

app.listen(3000,()=>{
console.log("listening to port 3000");
})
23 changes: 23 additions & 0 deletions backend/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
const jwt=require("jsonwebtoken");
const JWT_SECRET=require("./config");

const authMiddleware=(req,res,next)=>{
const authHeader=req.headers.authorization;
if(!authHeader || !authHeader.startsWith("Bearer ")){
return res.json({msg:"invalid token"})
}

const token=authHeader.split(' ')[1];
try{
const decoded=jwt.verify(token,JWT_SECRET);
req.userId=decoded.userId;
next();
}
catch(err){
console.log(err);
return res.json({msg:"error in jwt verification"})
}
}
module.exports={
authMiddleware
}
196 changes: 161 additions & 35 deletions backend/package-lock.json

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

6 changes: 4 additions & 2 deletions backend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@
"author": "",
"license": "ISC",
"dependencies": {
"cors": "^2.8.5",
"express": "^4.18.2",
"mongoose": "^8.1.0",
"zod": "^3.22.4"
"jsonwebtoken": "^9.0.2",
"mongoose": "^8.5.1",
"zod": "^3.23.8"
}
}
Loading