diff --git a/sockets/index.js b/sockets/index.js index 3673838..2618813 100644 --- a/sockets/index.js +++ b/sockets/index.js @@ -5,5 +5,6 @@ module.exports = socket => { socket.on("checkResend", require("./email/checkResend")(socket)); socket.on("forgotPassword", require("./email/forgotPassword")(socket)); socket.on("setPassword", require("./email/setPassword")(socket)); + socket.on("profileEdit", require("./profile/edit")(socket)); socket.emit("connected"); } diff --git a/sockets/profile/edit.js b/sockets/profile/edit.js new file mode 100644 index 0000000..36d3f2f --- /dev/null +++ b/sockets/profile/edit.js @@ -0,0 +1,22 @@ +const models = require("../../models"); + +module.exports = socket => { + return async (data) => { + let user = await models.User.findByPk(data.email); + if (!user) + socket.emit("profileEdit", {error: {message: "not_found"}}); + else if (!user.checkPassword(data.oldPassword)) + socket.emit("profileEdit", {error: {message: "invalid_password"}}) + else { + if (data.firstName !== user.firstName) + user.firstName = data.firstName; + if (data.lastName !== user.lastName) + user.lastName = data.lastName; + if (data.password && !user.checkPassword(data.password)) + user.passwordHash = data.password + socket.request.session.user = user; + socket.request.session.save(); + socket.emit("profileEdit", user) + } + } +}