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

Frontend done #40

Open
wants to merge 3 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
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_SECRETS: "paytm@prince"
}
49 changes: 49 additions & 0 deletions backend/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
const mongoose = require("mongoose");

mongoose.connect("mongodb+srv://prince981620:Hello%[email protected]/paytm");

const userSchema = new mongoose.Schema({
username: {
type: String,
required: true,
unique: true,
trim: true, // trim spaces from begininig and ends
lowercase: true,
minLength: 3,
maxLength: 30
},
password: {
type: String,
required: true,
minLength: 6
},
firstName: {
type: String,
required: true,
trim: true,
maxLength: 50
},
lastName: {
type: String,
required: true,
maxLength: 50
}
})
const accountSchema = new mongoose.Schema({
userId: {
type: mongoose.Schema.Types.ObjectId,
ref: 'User',
required: true
},
balance: {
type: Number,
required: true
}
})
const Account = mongoose.model("account",accountSchema);
const User = mongoose.model("User",userSchema);

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 mainRouter = require("./routes/index");
const cors = require("cors");

const app = express();

app.use(cors());
app.use(express.json());
// all the req for /api/v1 goes to mainRouter i.e /routes/index.js
app.use("/api/v1",mainRouter);

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


const authMiddleware = (req,res,next)=>{
const authHeader = req.headers.authorization;
if(!authHeader || !authHeader.startsWith('Bearer ')){
return res.status(411).json({});
}
const token = authHeader.split(" ")[1];

try{
const decoded = jwt.verify(token,JWT_SECRETS);
if(decoded.userId){
req.userId = decoded.userId;
next();
}else{
return res.status(403).json({});
}
}catch(err){
return res.status(403),json({});
}
}
module.exports = {
authMiddleware
}
Loading