Skip to content

Commit

Permalink
Edit user profile route
Browse files Browse the repository at this point in the history
  • Loading branch information
ahendouz committed Mar 9, 2019
1 parent d3f502b commit e625f22
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 4 deletions.
4 changes: 2 additions & 2 deletions server/src/authentication/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const signup = async (

// Validation.
if (!isValid) {
res.status(400).json(errors);
return res.status(400).json(errors);
}
// Profile = Rider || Consumer.
let Profile;
Expand Down Expand Up @@ -56,7 +56,7 @@ const signin = async ({ body: { email, password } }, res) => {
// Validation.
const { errors, isValid } = validateSignin(email, password);
if (!isValid) {
res.status(400).json(errors);
return res.status(400).json(errors);
}
// Find the user.
const user = await User.findOne({ email });
Expand Down
1 change: 1 addition & 0 deletions server/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,6 @@ mongoose.set("useFindAndModify", false);
const port = process.env.PORT || 4000;
// Use our routes.
app.use("/api/users", users);
app.use("/api/profile", profile);

app.listen(port, () => console.log(`Server is running on port ${port} 🚀🚀🚀`));
38 changes: 38 additions & 0 deletions server/src/routes/api/profile.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,45 @@
const express = require("express");
const router = express.Router();
const bcrypt = require("bcryptjs");

const requireAuth = require("../../utils/requireAuth");

const validateProfile = require("../../validation/profile");

// GET - api/profile/test - Test Users Route.
router.get("/test", (req, res) => res.json({ msg: "Profile Works" }));

// POST = PRIVATE = api/porfile/edit_profile - edit user's profile.
router.post(
"/edit_profile",
requireAuth,
async ({ user: { id }, body: { name, email, password, avatar } }, res) => {
const { errors, isValid } = validateProfile(name, email, password);
console.log("🧩😘", errors);
if (!isValid) {
return res.status(400).json(errors);
}
bcrypt.genSalt(10, async (err, salt) => {
bcrypt.hash(password, salt, async (err, hash) => {
if (err) throw err;
// Hashong the new version
password = hash;

const profileFields = {
name,
email,
password,
avatar
};
const user = await User.findOneAndUpdate(
{ _id: id },
{ $set: profileFields },
{ new: true }
);
res.status(200).json({ msg: "Your profile is updated" });
});
});
}
);

module.exports = router;
3 changes: 1 addition & 2 deletions server/src/routes/api/users.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ router.post("/signup/:type", signup);
// POST - api/users/signin/:type - Signin user
router.post("/signin", signin);

// POST - api/users/signin - Authorize user via facebook.
// router.post("/oauth/facebook", requireFbAuth, facebookOAuth);
// POST - api/users/oauth/facebook/:type - Authorize user via facebook.
router.post("/oauth/facebook/:type", requireFbAuth, facebookOAuth);

module.exports = router;
2 changes: 2 additions & 0 deletions server/src/utils/requireAuth.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const passport = require("passport");
module.exports = passport.authenticate("jwt", { session: false });
36 changes: 36 additions & 0 deletions server/src/validation/profile.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
const Validator = require("validator");
const isEmpty = require("./isEmpty");

module.exports = function validateProfile(name, email, password) {
let errors = {};

name = !isEmpty(name) ? name : "";
email = !isEmpty(email) ? email : "";
password = !isEmpty(password) ? password : "";

if (!Validator.isLength(name, { min: 2, max: 30 })) {
errors.name = "Name must be between 2 and 30 characters";
}
if (Validator.isEmpty(name)) {
errors.name = "Name field is required";
}
if (!Validator.isEmail(email)) {
errors.email = "Email is invalid";
}

if (Validator.isEmpty(email)) {
errors.email = "Email field is required";
}

if (!Validator.isLength(password, { min: 6, max: 30 })) {
errors.password = "Password must be at least 6 characters";
}
if (Validator.isEmpty(password)) {
errors.password = "Password field is required";
}

return {
errors,
isValid: isEmpty(errors)
};
};

0 comments on commit e625f22

Please sign in to comment.