-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
80 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
}; | ||
}; |