From 6477d8e07467b593caf34712629a846ded03d1cf Mon Sep 17 00:00:00 2001 From: flifloo Date: Mon, 2 Nov 2020 17:18:03 +0100 Subject: [PATCH] Add profile edit --- sockets/index.js | 1 + sockets/profile/edit.js | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+) create mode 100644 sockets/profile/edit.js 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) + } + } +}