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

UpdateMe #2

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
65 changes: 22 additions & 43 deletions controllers/userController.js
Original file line number Diff line number Diff line change
@@ -1,60 +1,39 @@
const User = require('../models/User');

// Change password
exports.changePassword = async (req, res) => {
const { oldPassword, newPassword } = req.body;
// Update user information
exports.updateMe = async (req, res) => {
const { oldPassword, newPassword, newEmail, newPhoneNumber } = req.body;

try {
const user = await User.findById(req.user._id);

if (user && (await user.matchPassword(oldPassword))) {
user.password = newPassword;
await user.save();

res.json({ message: 'Password updated successfully' });
} else {
res.status(401).json({ message: 'Incorrect old password' });
if (!user) {
return res.status(404).json({ message: 'User not found' });
}
} catch (error) {
res.status(500).json({ message: error.message });
}
};

// Update email
exports.updateUserEmail = async (req, res) => {
const { newEmail } = req.body;

try {
const user = await User.findById(req.user._id);
// Update password if provided
if (oldPassword && newPassword) {
if (await user.matchPassword(oldPassword)) {
user.password = newPassword;
} else {
return res.status(401).json({ message: 'Incorrect old password' });
}
}

if (user) {
// Update email if provided
if (newEmail) {
user.email = newEmail;
await user.save();

res.json({ message: 'Email updated successfully' });
} else {
res.status(404).json({ message: 'User not found' });
}
} catch (error) {
res.status(500).json({ message: error.message });
}
};

// Update phone number
exports.updateUserPhone = async (req, res) => {
const { newPhoneNumber } = req.body;

try {
const user = await User.findById(req.user._id);

if (user) {
// Update phone number if provided
if (newPhoneNumber) {
user.phoneNumber = newPhoneNumber;
await user.save();

res.json({ message: 'Phone number updated successfully' });
} else {
res.status(404).json({ message: 'User not found' });
}

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

res.json({ message: 'User information updated successfully' });
} catch (error) {
res.status(500).json({ message: error.message });
}
Expand Down
Empty file added gigg.drawio.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.