Mise à jour de routes et models
This commit is contained in:
+58
-54
@@ -4,90 +4,94 @@ const ErrorResponse = require("../util/errorResponse");
|
||||
const { sign } = require("../util/jwt");
|
||||
|
||||
module.exports.createUser = asyncHandler(async (req, res, next) => {
|
||||
const { email, password, username } = req.body.user;
|
||||
const { email, password, username, firstname, lastname } = req.body.user;
|
||||
|
||||
fieldValidation(email, next);
|
||||
fieldValidation(password, next);
|
||||
fieldValidation(username, next);
|
||||
fieldValidation(email, next);
|
||||
fieldValidation(password, next);
|
||||
fieldValidation(username, next);
|
||||
fieldValidation(firstname, next);
|
||||
fieldValidation(lastname, next);
|
||||
|
||||
const user = await User.create({
|
||||
email: email,
|
||||
password: password,
|
||||
username: username,
|
||||
});
|
||||
const user = await User.create({
|
||||
email: email,
|
||||
password: password,
|
||||
username: username,
|
||||
firstname: firstname,
|
||||
lastname: lastname,
|
||||
});
|
||||
|
||||
if (user.dataValues.password) {
|
||||
delete user.dataValues.password;
|
||||
}
|
||||
if (user.dataValues.password) {
|
||||
delete user.dataValues.password;
|
||||
}
|
||||
|
||||
user.dataValues.token = await sign(user);
|
||||
user.dataValues.token = await sign(user);
|
||||
|
||||
user.dataValues.bio = null;
|
||||
user.dataValues.image = null;
|
||||
user.dataValues.bio = null;
|
||||
user.dataValues.image = null;
|
||||
|
||||
res.status(201).json({ user });
|
||||
res.status(201).json({ user });
|
||||
});
|
||||
|
||||
module.exports.loginUser = asyncHandler(async (req, res, next) => {
|
||||
const { email, password } = req.body.user;
|
||||
const { email, password } = req.body.user;
|
||||
|
||||
fieldValidation(email, next);
|
||||
fieldValidation(password, next);
|
||||
fieldValidation(email, next);
|
||||
fieldValidation(password, next);
|
||||
|
||||
const user = await User.findOne({
|
||||
where: {
|
||||
email: email,
|
||||
},
|
||||
});
|
||||
const user = await User.findOne({
|
||||
where: {
|
||||
email: email,
|
||||
},
|
||||
});
|
||||
|
||||
if (!user) {
|
||||
return next(new ErrorResponse(`User not found`, 404));
|
||||
}
|
||||
if (!user) {
|
||||
return next(new ErrorResponse(`User not found`, 404));
|
||||
}
|
||||
|
||||
const isMatch = user.matchPassword(password);
|
||||
const isMatch = user.matchPassword(password);
|
||||
|
||||
if (!isMatch) {
|
||||
return next(new ErrorResponse("Wrong password", 401));
|
||||
}
|
||||
if (!isMatch) {
|
||||
return next(new ErrorResponse("Wrong password", 401));
|
||||
}
|
||||
|
||||
delete user.dataValues.password;
|
||||
delete user.dataValues.password;
|
||||
|
||||
user.dataValues.token = await sign(user);
|
||||
user.dataValues.token = await sign(user);
|
||||
|
||||
user.dataValues.bio = null;
|
||||
user.dataValues.image = null;
|
||||
user.dataValues.bio = null;
|
||||
user.dataValues.image = null;
|
||||
|
||||
res.status(200).json({ user });
|
||||
res.status(200).json({ user });
|
||||
});
|
||||
|
||||
module.exports.getCurrentUser = asyncHandler(async (req, res, next) => {
|
||||
const { loggedUser } = req;
|
||||
const user = await User.findByPk(loggedUser.id);
|
||||
const { loggedUser } = req;
|
||||
const user = await User.findByPk(loggedUser.id);
|
||||
|
||||
if (!user) {
|
||||
return next(new ErrorResponse(`User not found`, 404));
|
||||
}
|
||||
if (!user) {
|
||||
return next(new ErrorResponse(`User not found`, 404));
|
||||
}
|
||||
|
||||
user.dataValues.token = req.headers.authorization.split(" ")[1];
|
||||
user.dataValues.token = req.headers.authorization.split(" ")[1];
|
||||
|
||||
res.status(200).json({ user });
|
||||
res.status(200).json({ user });
|
||||
});
|
||||
|
||||
module.exports.updateUser = asyncHandler(async (req, res, next) => {
|
||||
await User.update(req.body.user, {
|
||||
where: {
|
||||
id: req.user.id,
|
||||
},
|
||||
});
|
||||
await User.update(req.body.user, {
|
||||
where: {
|
||||
id: req.user.id,
|
||||
},
|
||||
});
|
||||
|
||||
const user = await User.findByPk(req.user.id);
|
||||
user.dataValues.token = req.headers.authorization.split(" ")[1];
|
||||
const user = await User.findByPk(req.user.id);
|
||||
user.dataValues.token = req.headers.authorization.split(" ")[1];
|
||||
|
||||
res.status(200).json({ user });
|
||||
res.status(200).json({ user });
|
||||
});
|
||||
|
||||
const fieldValidation = (field, next) => {
|
||||
if (!field) {
|
||||
return next(new ErrorResponse(`Missing fields`, 400));
|
||||
}
|
||||
if (!field) {
|
||||
return next(new ErrorResponse(`Missing fields`, 400));
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user