Skip to content

Commit

Permalink
Merge branch 'main' into Isha_contributer
Browse files Browse the repository at this point in the history
  • Loading branch information
hustlerZzZ authored Jun 15, 2024
2 parents 417a892 + dba1004 commit ee25dd4
Show file tree
Hide file tree
Showing 31 changed files with 1,195 additions and 756 deletions.
21 changes: 21 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Vansh Waldeo

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
1 change: 0 additions & 1 deletion server/config/database.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ require("dotenv").config();

const dbConnect = () =>{
mongoose.connect(process.env.DATABASE_URL,{

useNewUrlParser : true,
useUnifiedTopology : true,
})
Expand Down
177 changes: 56 additions & 121 deletions server/controllers/Auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,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 @@ -42,10 +45,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 @@ -103,19 +103,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 @@ -154,6 +145,7 @@ exports.studentLogin = async (req, res) => {
user,
});
} else {

return res.status(403).json({
success: false,
message: "Pasword Incorrect",
Expand Down Expand Up @@ -194,9 +186,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 @@ -216,17 +206,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 @@ -235,10 +219,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 @@ -254,16 +235,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 @@ -276,10 +249,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 @@ -297,9 +267,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 @@ -343,19 +317,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 @@ -436,9 +401,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 @@ -458,19 +421,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 @@ -479,10 +434,7 @@ exports.changeCanteenPassword = async (
});
}

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

user.password = newHashedPassword;
user.save();
Expand Down Expand Up @@ -517,30 +469,26 @@ exports.saveContactMessage = async (req, res) => {
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 @@ -563,14 +511,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 @@ -630,23 +575,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 @@ -660,10 +598,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!");
}
};
Expand Down
Loading

0 comments on commit ee25dd4

Please sign in to comment.