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

userRoutes done #26

Open
wants to merge 6 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: 2 additions & 0 deletions backend/config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const JWT_SECRET = "ajitsecret";
module.exports = JWT_SECRET;
44 changes: 44 additions & 0 deletions backend/db/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
const mongoose=require("mongoose");
const { Schema, string } = require("zod");

mongoose.connect("mongodb+srv://ajit:Uzumaki%[email protected]/paytm")

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 accountSchema=new mongoose.Schema({
userId:{type:mongoose.Schema.Types.ObjectId,ref:"user",required:true},
balance:{
type:Number,
required:true
}
})
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 app = express();
const cors = require("cors");
const MainRouter = require("./router/index");



app.use(cors());
app.use(express.json());
app.use("/api/v1", MainRouter);

app.listen(3000, () => {
console.log("Server is running on port 3000");
});
26 changes: 26 additions & 0 deletions backend/middleware/Authmiddleware.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);
console.log(decoded)
req.userId = decoded.UserId;
console.log(req.userId)
next();
} catch (err) {
return res.status(403).json({});
}
};

module.exports = {
authMiddleware
}
Loading