Skip to content
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
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
1 change: 1 addition & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
.env
2 changes: 2 additions & 0 deletions backend/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@


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

mongoose.connect(
dburl
);

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: 50,
},
lastname: { type: String, required: true, trim: true, maxLength: 50 },
});

const accschema = new mongoose.Schema({
userId:{
type:mongoose.Types.ObjectId,
ref:"User",
required:true
},
balance:{
type:Number,
required:true
}
})

const User = mongoose.model("User", userschema);
const Balance = mongoose.model("Balance",accschema)
module.exports = {
User,
Balance
};
33 changes: 33 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,36 @@
const express = require("express");
const mainRouter = require("./routes/index_routes");
const cors = require("cors")
const authvalidator = require("./middlewere")
const app = express();
app.use(express.json());
app.use(cors())

// Use the main router
app.use("/app/v1", mainRouter);

// Root route
app.get("/", (req, res) => {
res.json({
msg: "Hey there, how are you!",
});
});

app.post("/protected",authvalidator,(req,res)=>{
res.json({
message:"this is protected site"
})
})


app.use((err,req,res,next)=>{
console.log("this is your err : " +err)
res.send("something went wrong")
})



// Start the server
app.listen(3000, () => {
console.log("App is live on http://localhost:3000");
});
24 changes: 24 additions & 0 deletions backend/middlewere.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
const jwtpass = process.env.JWT_SEC
const jwt = require('jsonwebtoken')



const authvalidator = (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 decode = jwt.verify(token,jwtpass)
req.userId = decode.userId
next()
}
catch(e){
return res.status(403).json({})
}


}

module.exports = authvalidator
Loading