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

feat: email validation middleware added (#381) #387

Merged
merged 6 commits into from
Oct 6, 2023
Merged
Changes from 2 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
194 changes: 92 additions & 102 deletions server/controllers/userController.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,125 +3,115 @@ const validator = require('validator').default;

const User = require('../models/UserSchema');

const loginUser = async (req, res) => {
const { email } = req.body;

if (typeof email !== 'string' || !validator.isEmail(email)) {
res.status(406).json({
message: 'Email is invalid',
});
// Defining separate email validation middleware
const emailValidator = (req, res, next) => {
const { email } = req.body;

if (typeof email !== 'string' || !validator.isEmail(email)) {
return res.status(406).json({
message: 'Email is invalid',
});
} else {
next();
}
};

return;
}
const loginUser = async (req, res) => {
const { email } = req.body;

try {
let findUser = await User.findOne({ email });
try {
let findUser = await User.findOne({ email });

if (!findUser) {
const newUser = await User.create({ email });
if (!findUser) {
const newUser = await User.create({ email });

res.status(200).json({
id: newUser._id,
});
res.status(200).json({
id: newUser._id,
});

return;
}
return;
}

res.status(200).json({
id: findUser._id,
});
} catch (err) {
res.status(500).json({
message: 'An error occured whiles logging in',
});
}
res.status(200).json({
id: findUser._id,
});
} catch (err) {
res.status(500).json({
message: 'An error occured while logging in',
});
}
};

const getProfile = async (req, res) => {
try {
const { email } = req.params;

// Find the user by email
const user = await User.findOne({ email });

if (!user) {
return res.status(404).json({ error: 'User not found' });
}

// Send the user profile data as JSON response
res.status(200).json(user);
} catch (error) {
console.error('Error fetching user profile:', error);
res.status(500).json({ error: 'Internal server error' });
}
try {
const { email } = req.params;

// Find the user by email
const user = await User.findOne({ email });

if (!user) {
return res.status(404).json({ error: 'User not found' });
}

// Send the user profile data as JSON response
res.status(200).json(user);
} catch (error) {
console.error('Error fetching user profile:', error);
res.status(500).json({ error: 'Internal server error' });
}
Dun-sin marked this conversation as resolved.
Show resolved Hide resolved
};

const updateProfile = async (req, res) => {
const { username, aboutMe, gender, age, email } = req.body;

if (typeof email !== 'string' || !validator.isEmail(email)) {
res.status(406).json({
message: 'Email is invalid',
});

return;
}

try {
// Find the user by email
const user = await User.findOne({ email });

if (!user) {
return res.status(404).json({ error: 'User not found' });
}

// Update user's profile with provided fields or defaults
user.username = username || 'Anonymous';
user.aboutMe = aboutMe || null;
user.gender = gender || 'Unknown';
user.age = age || null;

// Save the updated user profile
await user.save();

return res.status(200).json({ message: 'Profile updated successfully' });
} catch (error) {
console.error(error);
return res.status(500).json({ error: 'Internal server error' });
}
const { username, aboutMe, gender, age, email } = req.body;

try {
// Find the user by email
const user = await User.findOne({ email });

if (!user) {
return res.status(404).json({ error: 'User not found' });
}

// Update user's profile with provided fields or defaults
user.username = username || 'Anonymous';
user.aboutMe = aboutMe || null;
user.gender = gender || 'Unknown';
user.age = age || null;

// Save the updated user profile
await user.save();

return res.status(200).json({ message: 'Profile updated successfully' });
} catch (error) {
console.error(error);
return res.status(500).json({ error: 'Internal server error' });
}
};

const deleteUser = async (req, res) => {
const { email } = req.body;

if (typeof email !== 'string' || !validator.isEmail(email)) {
res.status(406).json({
message: 'Email is invalid',
});

return;
}
try {
// Find the user by email
const user = await User.findOne({ email });

if (!user) {
return res.status(404).json({ error: 'User not found' });
}

// Delete the user
await user.deleteOne();

return res.status(200).json({ message: 'User deleted successfully' });
} catch (error) {
console.error(error);
return res.status(500).json({ error: 'Internal server error' });
}
const { email } = req.body;

try {
// Find the user by email
const user = await User.findOne({ email });

if (!user) {
return res.status(404).json({ error: 'User not found' });
}

// Delete the user
await user.deleteOne();

return res.status(200).json({ message: 'User deleted successfully' });
} catch (error) {
console.error(error);
return res.status(500).json({ error: 'Internal server error' });
}
};

UserRouter.route('/login').post(loginUser);
UserRouter.route('/profile').post(updateProfile);
UserRouter.route('/login').post(emailValidator, loginUser);
UserRouter.route('/profile').post(emailValidator, updateProfile);
UserRouter.route('/profile/:email').get(getProfile);
UserRouter.route('/deleteUser').delete(deleteUser);
UserRouter.route('/deleteUser').delete(emailValidator, deleteUser); //Email validation applied to the required request handlers

module.exports = UserRouter;
Loading