Skip to content

Commit

Permalink
Merge pull request #71 from buildingu/FE-feature-add-forgot-password-…
Browse files Browse the repository at this point in the history
…page

Fe feature add forgot password page
  • Loading branch information
Sidragon123 authored Dec 17, 2024
2 parents b1e161d + f6abf4f commit 51423e4
Show file tree
Hide file tree
Showing 8 changed files with 281 additions and 65 deletions.
25 changes: 13 additions & 12 deletions Controllers/passwordController.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
require("dotenv").config();
const db = require("../Models/index");
const bcrypt = require("bcrypt");
const saltRounds = 10;
const saltRounds = parseInt(process.env.SALT_ROUNDS);
const Users = db.User;
const Token = db.Otptoken;
const { tokenValidator } = require("./helpers/tokenValidator");
const { sendOTP } = require("../utility/email/email");
const updatepasswordValidator = require("../utility/inputValidator/updatepasswordValidator")
const updatepasswordValidator = require("../utility/inputValidator/updatepasswordValidator");

const sendToken = async (req, res) => {
const { username } = req.body;
Expand Down Expand Up @@ -85,28 +85,29 @@ const updatePassword = async (req, res) => {
try {
const { username, resetToken, newPassword } = req.body;
const { errors, validationCheck } = updatepasswordValidator(req.body);

const userInDB = await Users.findOne({ where: { username: username } });
const userInTokenDB = await Token.findOne({ where: { username: username } });
const userInTokenDB = await Token.findOne({
where: { username: username },
});

const isTokenAndUserValid = await tokenValidator(username, resetToken);

const hashednewPassword = await bcrypt.hash(newPassword, saltRounds);


if (!validationCheck) {
res.status(400).json(errors);
return;
}


if (isTokenAndUserValid.success) {
await userInDB.update(
{ password: hashednewPassword },
{ where: { username: username } }
).then( async()=>{
await userInTokenDB.destroy();
});
await userInDB
.update(
{ password: hashednewPassword },
{ where: { username: username } }
)
.then(async () => {
await userInTokenDB.destroy();
});
res.status(200).json({ msg: "Password has been successfully Changed" });
} else {
res.status(400).json({
Expand Down
22 changes: 7 additions & 15 deletions Controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const db = require("../Models/index");
const bcrypt = require("bcrypt");
const cookie = require("cookie");
const jwt = require("jsonwebtoken");
const saltRounds = 10;
const saltRounds = parseInt(process.env.SALT_ROUNDS);
const Users = db.User;
const ExerciseInfo = db.ExerciseInfo;
const FeedbackRequest = db.FeedbackRequest;
Expand Down Expand Up @@ -44,7 +44,7 @@ const registerUser = async (req, res) => {
// Hash the password
// Ideally adding a callback in the hash is best practice
//Might add callback as code
const hashedPassword = await bcrypt.hash(password, saltRounds);
const hashedPassword = await bcrypt.hash(password, saltRounds);
const userData = {
fName: fName,
username: userName,
Expand Down Expand Up @@ -194,26 +194,18 @@ const updateAccount = async (req, res) => {
.status(400)
.json({ msg: "Old password cannot be same as new password" });
}
updates.password = await bcrypt.hash(newPassword, saltRounds);
updates.password = bcrypt.hash(newPassword, saltRounds);
}

await isUserExist.update(updates);

// Since the full name is used in different tables and is called different names across tables, we are simply updating them below.
//Probably should have stuck to a naming scheme
if (fName) {
await FeedbackRequest.update(
{ whoisAssigned: fName },
{ where: { mentorId: isUserExist.mentorId } }
);
await FeedbackRequest.update(
{ studentName: fName },
{ where: { id: id } }
);
await ExerciseInfo.update(
{ internName: fName },
{ where: { userId: id } }
);
await FeedbackRequest.update({ studentName: fName }, { where: { userId: id } });
await FeedbackRequest.update({ mentorId: id }, { where: { whoisAssigned: fName } });

await ExerciseInfo.update({ internName: fName }, { where: { userId: id } });
await Feedbacks.update({ mentorName: fName }, { where: { userId: id } });
}

Expand Down
2 changes: 1 addition & 1 deletion Routes/Password.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ const passwordController = require('../Controllers/passwordController');

router.post("/forgotPassword", passwordController.sendToken);

router.get("/checkToken", passwordController.checkToken);
router.post("/checkToken", passwordController.checkToken);

router.patch("/updatePassword", passwordController.updatePassword);

Expand Down
73 changes: 36 additions & 37 deletions utility/inputValidator/updatepasswordValidator.js
Original file line number Diff line number Diff line change
@@ -1,41 +1,40 @@
module.exports = (input) => {
let errors = {};
let emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;

if (!input) {
return {
errors: { msg: "Input is required" },
validationCheck: false,
};
}

for (let [key, value] of Object.entries(input)) {
if (typeof value !== "string") {
errors[key] = `email and password must be a string`;
}
}

if (input.newPassword.length <= 0 || input.username.length <= 0) {
errors.field = "Password and or email Field Cannot be empty";
}

if (input.newPassword.length < 8) {
errors.password = "Password must be at least 8 characters long";
}

if (input.newPassword.length > 30) {
errors.password = "Password cannot be more than 30 characters long";
}

if (!emailPattern.test(input.username)) {
errors.email = "email is not valid";
}

let validationCheck = !Object.keys(errors).length ? true : false;

let errors = {};
let emailPattern = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}$/;

if (!input) {
return {
errors,
validationCheck,
errors: { msg: "Input is required" },
validationCheck: false,
};
}

for (let [key, value] of Object.entries(input)) {
if (typeof value !== "string") {
errors[key] = `email and password must be a string`;
}
}

if (input.newPassword.length <= 0 || input.username.length <= 0) {
errors.field = "Password and or email Field Cannot be empty";
}

if (input.newPassword.length < 8) {
errors.password = "Password must be at least 8 characters long";
}

if (input.newPassword.length > 30) {
errors.password = "Password cannot be more than 30 characters long";
}

if (!emailPattern.test(input.username)) {
errors.email = "email is not valid";
}

let validationCheck = !Object.keys(errors).length ? true : false;

return {
errors,
validationCheck,
};
};
2 changes: 2 additions & 0 deletions views/src/App.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import SingleFeedBack from "./Pages/SingleFeedbackPage";
import Homepage from "./Pages/HomePage";
import Unauthorized from "./Pages/Unauthorized";
import Notfound from "./Pages/Notfound";
import ForgotPassword from "./Pages/ForgotPassword";
import "@mantine/core/styles.css";
import { MantineProvider } from "@mantine/core";
import AuthWrapper from "./Utility/AuthWrapper";
Expand All @@ -21,6 +22,7 @@ function App() {
<Route path="/intern/*" element={<AuthWrapper>{({ user }) => <Interndashboard user={user} />}</AuthWrapper>} />
<Route path="/mentor/*" element={<AuthWrapper>{({ user }) => <Mentordashboard user={user} />}</AuthWrapper>} />
<Route path="/feedback/:id" element={<AuthWrapper>{({ user }) => <SingleFeedBack user={user} />}</AuthWrapper>} />
<Route path="/forgotpassword" element={< ForgotPassword/>} />
<Route path="/403" element={<Unauthorized />} />
{/* wasnt sure how to do this correctly but this route enables pages that dont match our routes to land
on the 404 page */}
Expand Down
Loading

0 comments on commit 51423e4

Please sign in to comment.