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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion config/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@ env.config();

const portNo = process.env.PORT_NO
const secretKey = process.env.SECRET_KEY
const db_url = process.env.DB_URL



module.exports = {
portNo,
secretKey
secretKey,
db_url
}
72 changes: 20 additions & 52 deletions controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,12 @@ const bcrypt = require("bcryptjs");
const jwt = require("jsonwebtoken");

exports.register_user = async (req, res) => {
const salt = 10;
const spassword = await bcrypt.hash(req.body.password, salt);

let user = new users({
username: req.body.username,
firstname: req.body.firstname,
lastname: req.body.lastname,
email: req.body.email,
password: spassword,
password: req.body.password,
mobile: req.body.mobile,
address: req.body.address,
});
Expand All @@ -23,7 +20,6 @@ exports.register_user = async (req, res) => {
exports.login_user = async (req, res) => {
const userData = await users.findOne({ email: req.body.email });
const pass = bcrypt.compare(userData.password, req.body.password);

const token = jwt.sign(
{ email: userData.email, id: userData._id },
config.secretKey
Expand All @@ -32,42 +28,27 @@ exports.login_user = async (req, res) => {
if (userData && pass) {
res.status(200).send(token);
} else {
return res
.status(401)
.send({ success: false, msg: "Email or Password is wrong" });
return res.status(401).send({ success: false, msg: "Email or Password is wrong" });
}
};

exports.changePass = async (req, res) => {
const { email } = jwt.verify(
req.token,
config.secretKey,
async (err, authData) => {
if (err) {
res.send({ result: "invalid token" });
} else {
const { password, new_password } = req.body;
if (password && new_password) {
if (password !== new_password) {
res.send({ status: "failed", message: "password dose not match" });
} else {
const salt = 10;
const spassword = await bcrypt.hash(req.body.password, salt);

await users.updateOne(email, {
password: spassword,
});
res.send({
status: "true",
message: "password reset successfully",
});
}
} else {
req.send({ status: "failed", message: "All fields are required" });
}
}
const { password, new_password } = req.body;
if (password && new_password) {
if (password !== new_password) {
res.send({ status: "failed", message: "password dose not match" });
} else {
await users.updateOne(req.data.email, {
password: password,
});
res.send({
status: "true",
message: "password reset successfully",
});
}
);
} else {
req.send({ status: "failed", message: "All fields are required" });
}
};
exports.forgetPass = async (req, res) => {
const { password, new_password, email } = req.body;
Expand All @@ -77,13 +58,11 @@ exports.forgetPass = async (req, res) => {
if (password !== new_password) {
res.send({ status: "failed", message: "password dose not match" });
} else {
const salt = 10;
const spassword = await bcrypt.hash(password, salt);

await users.updateOne(
{ email },
{
password: spassword,
password: password,
}
);
res.send({ status: "true", message: "password updated" });
Expand All @@ -97,24 +76,13 @@ exports.forgetPass = async (req, res) => {
};

exports.updateuser = async (req, res) => {
const { email } = jwt.verify(
req.token,
config.secretKey,
async (err, authData) => {
if (err) {
res.send({ result: "invalid token" });
}
}
);
const { username, firstname, lastname, password, mobile, uemail, address } = req.body;
const salt = 10;
const spassword = await bcrypt.hash(password, salt);

await users.updateOne(email, {
await users.updateOne(req.data.email, {
username: username,
firstname: firstname,
lastname: lastname,
password: spassword,
password: password,
mobile: mobile,
email: uemail,
address: address,
Expand Down
12 changes: 10 additions & 2 deletions dbconnection.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
const mongoose = require("mongoose");
const { db_url } = require("./config/config");

mongoose.connect("mongodb://localhost:27017/mydb1")

const db_connect = async ()=>{
try {
await mongoose.connect(db_url)
console.log("Database connected");
} catch (error) {
console.log("DB connection failed");
}
}
db_connect();
21 changes: 13 additions & 8 deletions middleware/mid_register.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ require("dotenv").config();
const mongoose = require("mongoose");
const users = mongoose.model("users");
const jwt = require("jsonwebtoken");
const config = require("../config/config");

exports.REG_MIDDLE = async (req, res, next) => {
const userData = await users.findOne({ email: req.body.email });
Expand All @@ -14,16 +15,20 @@ exports.REG_MIDDLE = async (req, res, next) => {
};

exports.checkAuth = async (req, res, next) => {
const bearerHeader = req.headers['authorization'];
if(typeof bearerHeader !== 'undefined'){
const bearerHeader = req.headers["authorization"];
if (typeof bearerHeader !== "undefined") {
const bearer = bearerHeader.split(" ");
const token = bearer[1];
req.token = token;
const {email}=jwt.verify(token, config.secretKey, async (err, authData) => {
if (err) {
res.send({ result: "invalid token" });
}
});
req.data= {email, token}
next();
}else{
} else {
res.send({
result:"Token is not valid"
})
result: "Token is not valid",
});
}

};
};
12 changes: 12 additions & 0 deletions model/userModel.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const bcrypt = require("bcryptjs");
const mongoose = require("mongoose");
const user = mongoose.Schema({
username:{
Expand Down Expand Up @@ -30,4 +31,15 @@ const user = mongoose.Schema({
},
});

user.pre('save',async function(next){
try {
const salt = 10;
const hashedpassword = await bcrypt.hash(this.password, salt);
this.set("password",hashedpassword)
next()
} catch (error) {
next(error)
}
});

module.exports = mongoose.model("users",user)
2 changes: 1 addition & 1 deletion routes/register.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ router.use('/updateuser',mid_register.checkAuth)
router.get("/register",mid_register.REG_MIDDLE,userController.register_user);
router.post("/auth/signin",userController.login_user);
router.put("/changePassword",userController.changePass);
router.put("/forgetPassword",userController.forgetPass);
router.put("/forgetpassword",userController.forgetPass);
router.put("/updateuser",userController.updateuser);

module.exports=router;