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

resolving the issue #30

Open
wants to merge 5 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
23 changes: 23 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js

# testing
/coverage

# production
/build

# misc
.DS_Store
.env.local
.env.development.local
.env.test.local
.env.production.local

npm-debug.log*
yarn-debug.log*
yarn-error.log*
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_SECRET: "secret",
};
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");

mongoose.connect(
"mongodb+srv://tanishsingla51:9313111030Aa%[email protected]/paytm"
);

const userSchema = new mongoose.Schema({
username: {
type: String,
required: true,
lowercase: true,
unique: true,
minLength: 3,
maxLength: 20,
},
password: {
type: String,
required: true,
minLength: 6,
},
firstName: {
type: String,
required: true,
minLength: 3,
maxLength: 20,
},
lastName: {
type: String,
required: true,
minLength: 3,
maxLength: 20,
},
});

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

const app = express();

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

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

app.listen(3000, () => {
console.log("Server is running on port 3000");
});
30 changes: 30 additions & 0 deletions backend/middleware.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
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({
msg: "Unauthorized",
});
}

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({
msg: err.message,
});
}
};

module.exports = {
authMiddleware,
};
Loading