Skip to content

Commit

Permalink
error rectified, don't use next two times in a middleware :))
Browse files Browse the repository at this point in the history
  • Loading branch information
Sid-bit28 committed Jun 20, 2023
1 parent 5aa4b78 commit f377d50
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 13 deletions.
27 changes: 18 additions & 9 deletions controllers/authController.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,30 @@ const login = async (req, res) => {
};

const updateUser = async (req, res) => {
const { email, name, lastName, skill } = req.body;
if (!email || !name || !lastName || !skill) {
throw new BadRequestError('Please provide all values');
// console.log(req.body);
// res.send('updateUser');
if (
!req.body.name ||
!req.body.email ||
!req.body.lastName ||
!req.body.skill
) {
throw new BadRequestError('Please provide all the values');
}

const user = await User.findOne({ _id: req.user.userId });
user.email = email;
user.name = name;
user.lastName = lastName;
user.skill = skill;

console.log(user);
user.email = req.body.email;
user.name = req.body.name;
user.lastName = req.body.lastName;
user.skill = req.body.skill;

await user.save();

const token = user.createJWT();
res.status(StatusCodes.OK).json({ user, token, skill: user.skill });

console.log({ user, token, skill: user.skill });
res.status(StatusCodes.OK).send({ user, token, skill: user.skill });
};

export { register, login, updateUser };
2 changes: 0 additions & 2 deletions middleware/auth.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ const auth = (req, res, next) => {
throw new UnAuthenticatedError('Authentication Invalid');
}
const token = authHeader.split(' ')[1];
// console.log(token);
try {
const payload = jwt.verify(token, process.env.JWT_SECRET);
req.user = { userId: payload.userId };
next();
} catch (error) {
throw new UnAuthenticatedError('Authentication Invalid');
}
Expand Down
6 changes: 4 additions & 2 deletions models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,10 @@ const UserSchema = new mongoose.Schema({

// generate hash password
UserSchema.pre('save', async function () {
// const salt = await bcrypt.genSalt(10);
// this.password = await bcrypt.hash(this.password, salt);
// console.log(this.modifiedPaths());
if (!this.isModified('password')) return;
const salt = await bcrypt.genSalt(10);
this.password = await bcrypt.hash(this.password, salt);
});

UserSchema.methods.createJWT = function () {
Expand Down

0 comments on commit f377d50

Please sign in to comment.