1
0
Fork 0
This repository has been archived on 2024-02-17. You can view files and clone it, but cannot push or open issues or pull requests.
letu/sockets/profile/edit.js

24 lines
944 B
JavaScript
Raw Normal View History

2020-11-02 17:18:03 +01:00
const models = require("../../models");
module.exports = socket => {
return async (data) => {
2020-11-03 12:54:40 +01:00
let user = await models.User.findByPk(socket.request.session.user.email);
2020-11-02 17:18:03 +01:00
if (!user)
socket.emit("profileEdit", {error: {message: "not_found"}});
else if (!user.checkPassword(data.oldPassword))
2020-12-12 18:20:02 +01:00
socket.emit("profileEdit", {error: {message: "invalid_password"}});
2020-11-02 17:18:03 +01:00
else {
if (data.firstName !== user.firstName)
user.firstName = data.firstName;
if (data.lastName !== user.lastName)
user.lastName = data.lastName;
user.newPassword = data.newPassword;
2020-11-02 17:18:03 +01:00
if (data.password && !user.checkPassword(data.password))
2020-12-12 18:20:02 +01:00
user.passwordHash = data.password;
2020-11-02 17:18:03 +01:00
socket.request.session.user = user;
socket.request.session.save();
socket.emit("profileEdit", user)
}
}
2020-12-12 18:20:02 +01:00
};