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

forgot and reset password done #5

Open
wants to merge 3 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 .env.example
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@

SENDGRID_API_KEY
MONGO_URI
SECRET_KEY
OTP_EXPIRE_TIME
JWT_EXPIRE_TIME


65 changes: 65 additions & 0 deletions controller/forgotPassword.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const {sendMail} = require('./sendmail_password')
const {User} = require('../model/User')



exports.forgotPassword = async (req, res) => {
try {
const user = await User.findOne({ personalEmail: req.body.personalEmail });

if (!user) {
return res.status(404).json({
success: false,
message: "User not found",
});

}


const resetPasswordToken = user.getresetpasswordtoken();

await user.save();

const resetUrl = `${req.protocol}://${req.get(
"host"
)}/password/reset/${resetPasswordToken}`;

const message =
{

to:user.personalEmail,
from: {
name: 'RK HALL IIT KGP',
email: 'harshit@kgpian.iitkgp.ac.in',
},
subject:"Reset Password Request",
html:`Reset Your Password by clicking on the link below: \n\n ${resetUrl}`
}

try {
await sendMail(message);

res.status(200).json({
success: true,
message: `Email sent to ${user.personalEmail}`,
});
}

catch (error) {
user.resetpasswordtoken = undefined;
user.resetpasswordexpire = undefined;
await user.save();

res.status(500).json({
success: false,
message: error.message,
});
}
} catch (error) {
res.status(500).json({
success: false,
message: error.message,
});
}
};

14 changes: 8 additions & 6 deletions controller/login.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
const bcrypt = require("bcryptjs");
const speakeasy = require("speakeasy");
const jwt = require("jsonwebtoken");
const { User } = require("../model/User");
const { sendMail } = require("./sendmail");
const { client } = require("../redis");

const bcrypt = require("bcryptjs")
const speakeasy = require("speakeasy")
const jwt = require("jsonwebtoken")
const { User } = require("../model/User")
const { sendMail } = require("./sendmail_otp")
const { client } = require("../redis")


exports.loginWithPassword = async (req, res) => {
try {
Expand Down
51 changes: 51 additions & 0 deletions controller/resetPassword.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
const crypto = require('crypto');
const {User} = require('../model/User')
const bcrypt = require('bcryptjs')


exports.resetPassword = async (req, res) => {
try {
const resetpasswordtoken = crypto
.createHash("sha256")
.update(req.params.token)
.digest("hex");

const user = await User.findOne({
resetpasswordtoken,
resetpasswordexpire: { $gte: Date.now() },
});

if (!user) {
return res.status(401).json({
success: false,
message: "Token is invalid or has expired",
});
}


user.password = req.body.password;










user.resetpasswordtoken = undefined;
user.resetpasswordexpire = undefined;
await user.save();

res.status(200).json({
success: true,
message: "Password Updated",
});
} catch (error) {
res.status(500).json({
success: false,
message: error.message,
});
}
};
File renamed without changes.
14 changes: 14 additions & 0 deletions controller/sendmail_password.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const sgMail = require('@sendgrid/mail')

require('dotenv').config()


exports.sendMail = async (message) => {
sgMail.setApiKey(process.env.SENDGRID_API_KEY)


sgMail.send(message).then((res) => {
console.log('message sent ....')
})
.catch((err) => { console.log(err) })
}
21 changes: 15 additions & 6 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ const { authenticateToken } = require("./middlewares/auth");
// const { upload } = require("./controller/update")

const { client } = require("./redis");
const { updatePassword } = require("./controller/updatePassword");
const { forgotPassword } = require("./controller/forgotPassword");
const { resetPassword } = require("./controller/resetPassword");

const app = express();
require("dotenv").config();

Expand All @@ -26,14 +28,21 @@ try {

app.use(cors());

app.use(express.json());
app.use(express.urlencoded({ extended: true }));
app.post("/login/password", loginWithPassword);
app.post("/login/otp", loginWithOtp);
app.post("/login/verify", verifyOTP);
app.use(express.json())
app.use(express.urlencoded({ extended: true }))

app.post("/login/password", loginWithPassword)
app.post("/login/otp", loginWithOtp)
app.post("/login/verify", verifyOTP)
// app.put("/update", authenticateToken , update )
app.post("/register", register);

app.post("/password/reset",forgotPassword);
app.put('/password/reset/:token',resetPassword);
app.put("/updateInfo",authenticateToken, updateInfo);
app.patch("/user/password", authenticateToken, updatePassword);
app.post("/register", register);

app.listen(8000, () => {
console.log("Listening on port 8000...")
})
Expand Down
Loading