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

Step-8 Completed #51

Open
wants to merge 9 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
1 change: 1 addition & 0 deletions backend/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
node_modules
dbcred.js
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 = "shubhamsinghsecret";
module.exports = { JWT_SECRET };
67 changes: 67 additions & 0 deletions backend/db.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
const mongoose = require('mongoose');
const bcrypt = require('bcrypt');
const connectionString = require('./routes/dbcred');

// const uri = connectionString;

mongoose.connect(connectionString);

const userSchema = new mongoose.Schema({
username: {
type: String,
required: true,
unique: true,
lowercase: true,
minLength: 3,
maxLength: 30,
trim: true
},
password_hash: {
type: String,
required: true
},
firstName: {
type: String,
required: true,
trim: true,
maxLength: 50
},
lastName: {
type: String,
required: true,
trim: true,
maxLength: 50
}
});

userSchema.statics.createHash = async (plainPassword) => {
const salt = await bcrypt.genSalt(2);
return await bcrypt.hash(plainPassword, salt);
}

userSchema.statics.validatePassword = async (candidatePassword, stored_password_hash) => {
// console.log(candidatePassword, stored_password_hash);
return await bcrypt.compare(candidatePassword, stored_password_hash);
}

const User = mongoose.model('User', userSchema);

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);

module.exports = ({
User,
Account
})
19 changes: 19 additions & 0 deletions backend/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,22 @@
const express = require("express");
const cors = require("cors");
const rootRouter = require("./routes/index");

const app = express();
const PORT = 3000;

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

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

app.get("/", (req, res) => {
return res.status(200).json({
message: "in index.js (main backend server)"
})
})

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

const authMiddleware = (req, res, next) => {
// if(!req) {
// console.log("req not defined");
// return
// }
const authHeader = req.headers.authorization;
if(!authHeader || !authHeader.startsWith('Bearer ')){
return res.status(403).json({
messsage: "Kya bhai auth header to bhejo??"
});
}
const token = authHeader.split(' ')[1];
if(token) {console.log("token found in auth-bearer header -> ", token);}

try{
const decoded = jwt.verify(token, JWT_SECRET);
if(decoded) {console.log("token decoded -> ", decoded);}

if(decoded.userId){
req.userId = decoded.userId;
console.log("next() to get executed next")
next();
} else {
return res.status(403).json({
messsage: "Cannot verify token"
});
}
} catch(err) {
console.log(err);
return res.status(403).json({
messsage: "check console for err in authMiddleware catch block"
});
}
}

module.exports = authMiddleware
Loading