From 15ddab9e0ece5221f549cb8f878803b8c26cf9b7 Mon Sep 17 00:00:00 2001 From: Haris Razis Date: Thu, 7 Jan 2021 16:53:17 +0200 Subject: [PATCH] =?UTF-8?q?=20=F0=9F=94=A8=20Fix=20update=20user?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Unpredictable behavior sometimes. To be fixed. --- server/index.js | 2 ++ server/routes/user.js | 33 ++++++++++++++++++ web/src/store/index.ts | 6 ++-- web/src/views/Login.vue | 2 +- web/src/views/Profile.vue | 70 +++++++++++++++++++++++++-------------- 5 files changed, 85 insertions(+), 28 deletions(-) create mode 100644 server/routes/user.js diff --git a/server/index.js b/server/index.js index 43afed5..80a9747 100644 --- a/server/index.js +++ b/server/index.js @@ -10,6 +10,7 @@ require('./services/socket')(server); const dataRoute = require('./routes/data') const authRoute = require('./routes/auth') +const userRoute = require('./routes/user') const athletesRoute = require('./routes/athletes') const passport = require('./services/passport'); @@ -42,6 +43,7 @@ app.use(passport.session(undefined)); app.use(dataRoute) app.use(authRoute) +app.use(userRoute) app.use(athletesRoute) const PORT = process.env.PORT || 8000; diff --git a/server/routes/user.js b/server/routes/user.js new file mode 100644 index 0000000..1375f40 --- /dev/null +++ b/server/routes/user.js @@ -0,0 +1,33 @@ +const express = require('express') +const router = express.Router(); +const mongoose = require('mongoose'); +const bcrypt = require('bcrypt'); +const {requireAuth} = require('../middlewares/middleware'); + +const User = mongoose.model('User') + +router.put('/api/user/:id', requireAuth, async (req, res) => { + const {username, email, password, newPassword} = req.body + + if (password && newPassword) { + bcrypt.compare(password, req.user.password, async (err, isMatch) => { + if (err) + return res.status(400).json({errors: 'Current password is wrong!'}); + + if (isMatch) { + const user = {username, email, newPassword} + await User.findByIdAndUpdate(req.params.id, user) + + res.send(req.user); + } + }); + } else if (username || email) { + const user = {username, email} + await User.findByIdAndUpdate(req.params.id, user) + + res.send(req.user); + } + +}); + +module.exports = router; diff --git a/web/src/store/index.ts b/web/src/store/index.ts index 26d3357..1d98e19 100644 --- a/web/src/store/index.ts +++ b/web/src/store/index.ts @@ -80,12 +80,12 @@ export default createStore({ updateUser({commit}, user) { return new Promise((resolve, reject) => { axios({ - method: 'post', - url: '/api/user', + method: 'put', + url: `/api/user/${user._id}`, data: qs.stringify({...user}) }) .then((resp: AxiosResponse) => { - commit("auth_success", resp.data.user); + commit("auth_success", resp.data); resolve(resp) }) .catch((err: Error) => { diff --git a/web/src/views/Login.vue b/web/src/views/Login.vue index 48ec35f..89eca60 100644 --- a/web/src/views/Login.vue +++ b/web/src/views/Login.vue @@ -22,7 +22,7 @@

-

+

diff --git a/web/src/views/Profile.vue b/web/src/views/Profile.vue index 8ce54fb..dd45ddb 100644 --- a/web/src/views/Profile.vue +++ b/web/src/views/Profile.vue @@ -1,6 +1,6 @@