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

[Bug]:"Confirm Password" field added in signup page #308

Merged
merged 4 commits into from
Jun 15, 2024
Merged
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
176 changes: 55 additions & 121 deletions server/controllers/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ exports.studentSignup = async (req, res) => {
console.log("This is jwt", process.env.JWT_SECRET);
try {
console.log(req.body);
const {
name,
email,
collegeName,
accountType,
password,
} = await req.body;
const { name, email, collegeName, accountType, password, confirmPassword } =
await req.body;

if (password !== confirmPassword) {
return res.status(400).json({
success: false,
message: "Password and Confirm password didn't match, try again",
});
}

const existingUser = await User.findOne({
email,
});
Expand All @@ -38,10 +41,7 @@ exports.studentSignup = async (req, res) => {
let hashedPassword;

try {
hashedPassword = await bcrypt.hash(
password,
10
);
hashedPassword = await bcrypt.hash(password, 10);
} catch (error) {
console.log(error);
return res.status(500).json({
Expand Down Expand Up @@ -99,19 +99,10 @@ exports.studentLogin = async (req, res) => {
accountType: user.accountType,
};

if (
await bcrypt.compare(
password,
user.password
)
) {
let token = jwt.sign(
payload,
process.env.JWT_SECRET,
{
expiresIn: "2h",
}
);
if (await bcrypt.compare(password, user.password)) {
let token = jwt.sign(payload, process.env.JWT_SECRET, {
expiresIn: "2h",
});

// creating a session
const session = new Session({
Expand Down Expand Up @@ -190,9 +181,7 @@ exports.studentLogout = async (req, res) => {

const token =
req.cookies?.token ||
req
?.header("Authorization")
?.replace("Bearer ", "");
req?.header("Authorization")?.replace("Bearer ", "");

if (token) {
await Session.findOneAndDelete({ token });
Expand All @@ -212,17 +201,11 @@ exports.studentLogout = async (req, res) => {
};

// Controller for changing the student password
exports.changeStudentPassword = async (
req,
res
) => {
exports.changeStudentPassword = async (req, res) => {
const { oldPassword, newPassword } = req.body;
const user = await User.findById(req.user._id);

const isPasswordCorrect = await bcrypt.compare(
oldPassword,
user.password
);
const isPasswordCorrect = await bcrypt.compare(oldPassword, user.password);

if (!isPasswordCorrect) {
return res.status(400).json({
Expand All @@ -231,10 +214,7 @@ exports.changeStudentPassword = async (
});
}

const newHashedPassword = await bcrypt.hash(
newPassword,
10
);
const newHashedPassword = await bcrypt.hash(newPassword, 10);

user.password = newHashedPassword;
user.save();
Expand All @@ -250,16 +230,8 @@ exports.changeStudentPassword = async (
exports.canteenSignup = async (req, res) => {
console.log("Received signup request with data:", req.body);
try {
const {
name,
email,
collegeName,
accountType,
password,
} = req.body;
const existingCanteen = await Canteen.findOne(
{ email }
);
const { name, email, collegeName, accountType, password } = req.body;
const existingCanteen = await Canteen.findOne({ email });

if (existingCanteen) {
console.log("User already exists with email:", email);
Expand All @@ -272,10 +244,7 @@ exports.canteenSignup = async (req, res) => {
let hashedPassword;

try {
hashedPassword = await bcrypt.hash(
password,
10
);
hashedPassword = await bcrypt.hash(password, 10);
} catch (error) {
console.error("Error in hashing password:", error);
return res.status(500).json({
Expand All @@ -293,9 +262,13 @@ exports.canteenSignup = async (req, res) => {
});

// Create a token
const token = jwt.sign({ id: canteen._id, email: canteen.email }, process.env.JWT_SECRET, {
expiresIn: '1h', // Set token expiration time as needed
});
const token = jwt.sign(
{ id: canteen._id, email: canteen.email },
process.env.JWT_SECRET,
{
expiresIn: "1h", // Set token expiration time as needed
}
);

console.log("User created successfully with ID:", canteen._id);
return res.status(200).json({
Expand Down Expand Up @@ -339,19 +312,10 @@ exports.canteenLogin = async (req, res) => {
accountType: canteen.accountType,
};

if (
await bcrypt.compare(
password,
canteen.password
)
) {
let token = jwt.sign(
payload,
process.env.JWT_SECRET,
{
expiresIn: "2h",
}
);
if (await bcrypt.compare(password, canteen.password)) {
let token = jwt.sign(payload, process.env.JWT_SECRET, {
expiresIn: "2h",
});
canteen = canteen.toObject();
canteen.token = token;
console.log(canteen);
Expand Down Expand Up @@ -432,9 +396,7 @@ exports.canteenLogout = async (req, res) => {

const token =
req.cookies?.token ||
req
?.header("Authorization")
?.replace("Bearer ", "");
req?.header("Authorization")?.replace("Bearer ", "");

if (token) {
await Session.findOneAndDelete({ token });
Expand All @@ -454,19 +416,11 @@ exports.canteenLogout = async (req, res) => {
};

// Canteen Reset Password
exports.changeCanteenPassword = async (
req,
res
) => {
exports.changeCanteenPassword = async (req, res) => {
const { oldPassword, newPassword } = req.body;
const user = await Canteen.findById(
req.user._id
);
const user = await Canteen.findById(req.user._id);

const isPasswordCorrect = await bcrypt.compare(
oldPassword,
user.password
);
const isPasswordCorrect = await bcrypt.compare(oldPassword, user.password);

if (!isPasswordCorrect) {
return res.status(400).json({
Expand All @@ -475,10 +429,7 @@ exports.changeCanteenPassword = async (
});
}

const newHashedPassword = await bcrypt.hash(
newPassword,
10
);
const newHashedPassword = await bcrypt.hash(newPassword, 10);

user.password = newHashedPassword;
user.save();
Expand All @@ -493,30 +444,26 @@ exports.changeCanteenPassword = async (
exports.forgotPassword = async (req, res) => {
try {
const { email } = req.body;
const existingUser = await findUserByEmail(
email
);
const existingUser = await findUserByEmail(email);

if (!existingUser) {
return res.status(400).json({
success: false,
message: "User does not exist",
});
} else {
const tokenReturn =
forgotPasswordToken(existingUser);
const tokenReturn = forgotPasswordToken(existingUser);
// const link = `http://localhost:3000/api/v1/newPassword/${existingUser._id}/${tokenReturn}`;

const link = `https://foodies-web-app.vercel.app/api/v1/newPassword/${existingUser._id}/${tokenReturn}`;

const transporter =
nodemailer.createTransport({
service: "gmail",
auth: {
user: process.env.EMAIL,
pass: process.env.MAILPASS,
},
});
const transporter = nodemailer.createTransport({
service: "gmail",
auth: {
user: process.env.EMAIL,
pass: process.env.MAILPASS,
},
});

const mailOptions = {
from: process.env.EMAIL,
Expand All @@ -539,14 +486,11 @@ exports.forgotPassword = async (req, res) => {
`,
};

await transporter.sendMail(
mailOptions,
function (error, info) {
if (error) {
console.log(error);
}
await transporter.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
}
);
});

res.status(201).json({
msg: "You should receive an email",
Expand Down Expand Up @@ -606,23 +550,16 @@ exports.resetPassword = async (req, res) => {
const oldUser = await findUserById(id);

if (!oldUser) {
return res
.status(404)
.json("User not found");
return res.status(404).json("User not found");
}

const verify = verifyToken(oldUser, token);
if (verify.id !== id) {
return res
.status(201)
.json({ change: false });
return res.status(201).json({ change: false });
}

const salt = await bcrypt.genSalt(10);
const newPassword = await bcrypt.hash(
password,
salt
);
const newPassword = await bcrypt.hash(password, salt);

if (oldUser instanceof User) {
await User.findByIdAndUpdate(id, {
Expand All @@ -636,10 +573,7 @@ exports.resetPassword = async (req, res) => {

res.status(201).json({ change: true });
} catch (error) {
console.log(
"Error while changing password: ",
error
);
console.log("Error while changing password: ", error);
res.status(500).json("Some error occurred!");
}
};
6 changes: 3 additions & 3 deletions server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@
"cors": "^2.8.5",
"datauri": "^4.1.0",
"dotenv": "^16.4.5",
"express": "^4.18.2",
"express": "^4.19.2",
"express-async-handler": "^1.2.0",
"faker": "^5.5.3",
"jsonwebtoken": "^9.0.2",
"mongoose": "^7.6.2",
"mongoose": "^7.6.13",
"multer": "^1.4.5-lts.1",
"nodemailer": "^6.9.13",
"nodemon": "^3.0.1",
"nodemon": "^3.1.3",
"path": "^0.12.7"
}
}
Loading