mirror of
https://github.com/ClaytonWWilson/CS307-Team24.git
synced 2025-12-16 10:18:48 +00:00
Fixing editProfile bugs and making the database storage more efficient
This commit is contained in:
parent
b17fb1f3f0
commit
8f5020f881
@ -125,7 +125,7 @@ exports.login = (req, res) => {
|
|||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
if (err.code === "auth/wrong-password" || err.code === "auth/invalid-email") {
|
if (err.code === "auth/wrong-password" || err.code === "auth/invalid-email" || err.code === "auth/user-not-found") {
|
||||||
return res
|
return res
|
||||||
.status(403)
|
.status(403)
|
||||||
.json({ general: "Invalid credentials. Please try again." });
|
.json({ general: "Invalid credentials. Please try again." });
|
||||||
@ -153,7 +153,7 @@ exports.updateProfileInfo = (req, res) => {
|
|||||||
// TODO: Add functionality for adding/updating profile images
|
// TODO: Add functionality for adding/updating profile images
|
||||||
|
|
||||||
// Data validation
|
// Data validation
|
||||||
const { valid, errors, profileData } = validateUpdateProfileInfo(req.body);
|
const { valid, errors, profileData } = validateUpdateProfileInfo(req);
|
||||||
if (!valid) return res.status(400).json(errors);
|
if (!valid) return res.status(400).json(errors);
|
||||||
|
|
||||||
// Update the database entry for this user
|
// Update the database entry for this user
|
||||||
|
|||||||
@ -9,23 +9,39 @@ const isEmpty = (str) => {
|
|||||||
else return false;
|
else return false;
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.validateUpdateProfileInfo = (data) => {
|
exports.validateUpdateProfileInfo = (req) => {
|
||||||
|
const newData = req.body;
|
||||||
|
// const oldData = req.userData;
|
||||||
let errors = {};
|
let errors = {};
|
||||||
let profileData = {};
|
let profileData = req.userData;
|
||||||
|
|
||||||
// ?: Should users be able to change their handles and emails?
|
// ?: Should users be able to change their handles and emails?
|
||||||
|
|
||||||
// Only adds the key to the database if the values are not empty
|
// Deletes any unused keys so that they aren't stored in the database
|
||||||
if (!isEmpty(data.firstName)) profileData.firstName = data.firstName.trim();
|
if (newData.firstName) {
|
||||||
if (!isEmpty(data.lastName)) profileData.lastName = data.lastName.trim();
|
profileData.firstName = newData.firstName.toString().trim();
|
||||||
if (!isEmpty(data.bio)) profileData.bio = data.bio.trim();
|
} else {
|
||||||
|
delete profileData.firstName;
|
||||||
|
}
|
||||||
|
|
||||||
if (isEmpty(data.email)) {
|
if (newData.lastName) {
|
||||||
|
profileData.lastName = newData.lastName.toString().trim();
|
||||||
|
} else {
|
||||||
|
delete profileData.lastName;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (newData.bio) {
|
||||||
|
profileData.bio = newData.bio.toString().trim();
|
||||||
|
} else {
|
||||||
|
delete profileData.bio;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (isEmpty(newData.email)) {
|
||||||
errors.email = "Must not be empty.";
|
errors.email = "Must not be empty.";
|
||||||
} else if (!isEmail(data.email)) {
|
} else if (!isEmail(newData.email)) {
|
||||||
errors.email = "Must be a valid email.";
|
errors.email = "Must be a valid email.";
|
||||||
} else {
|
} else {
|
||||||
profileData.email = data.email;
|
profileData.email = newData.email;
|
||||||
}
|
}
|
||||||
|
|
||||||
return {
|
return {
|
||||||
|
|||||||
@ -88,6 +88,14 @@ export class edit extends Component {
|
|||||||
handle: this.state.handle,
|
handle: this.state.handle,
|
||||||
bio: this.state.bio
|
bio: this.state.bio
|
||||||
};
|
};
|
||||||
|
|
||||||
|
// Removes all keys from newProfileData that are empty, undefined, or null
|
||||||
|
Object.keys(newProfileData).forEach(key => {
|
||||||
|
if (newProfileData[key] === "" || newProfileData[key] === undefined || newProfileData[key] === null) {
|
||||||
|
delete newProfileData[key];
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
axios
|
axios
|
||||||
.post("/updateProfileInfo", newProfileData)
|
.post("/updateProfileInfo", newProfileData)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user