Browse Source

🔨 Fix update user

Unpredictable behavior sometimes. To be fixed.
main
Haris Razis 4 years ago
parent
commit
15ddab9e0e
  1. 2
      server/index.js
  2. 33
      server/routes/user.js
  3. 6
      web/src/store/index.ts
  4. 2
      web/src/views/Login.vue
  5. 70
      web/src/views/Profile.vue

2
server/index.js

@ -10,6 +10,7 @@ require('./services/socket')(server);
const dataRoute = require('./routes/data') const dataRoute = require('./routes/data')
const authRoute = require('./routes/auth') const authRoute = require('./routes/auth')
const userRoute = require('./routes/user')
const athletesRoute = require('./routes/athletes') const athletesRoute = require('./routes/athletes')
const passport = require('./services/passport'); const passport = require('./services/passport');
@ -42,6 +43,7 @@ app.use(passport.session(undefined));
app.use(dataRoute) app.use(dataRoute)
app.use(authRoute) app.use(authRoute)
app.use(userRoute)
app.use(athletesRoute) app.use(athletesRoute)
const PORT = process.env.PORT || 8000; const PORT = process.env.PORT || 8000;

33
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;

6
web/src/store/index.ts

@ -80,12 +80,12 @@ export default createStore({
updateUser({commit}, user) { updateUser({commit}, user) {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
axios({ axios({
method: 'post', method: 'put',
url: '/api/user', url: `/api/user/${user._id}`,
data: qs.stringify({...user}) data: qs.stringify({...user})
}) })
.then((resp: AxiosResponse) => { .then((resp: AxiosResponse) => {
commit("auth_success", resp.data.user); commit("auth_success", resp.data);
resolve(resp) resolve(resp)
}) })
.catch((err: Error) => { .catch((err: Error) => {

2
web/src/views/Login.vue

@ -22,7 +22,7 @@
</p> </p>
</div> </div>
<div class="field"> <div class="field">
<p c lass="control"> <p class="control">
<button class="button is-info is-rounded is-medium" type="submit"> <button class="button is-info is-rounded is-medium" type="submit">
Login Login
</button> </button>

70
web/src/views/Profile.vue

@ -1,6 +1,6 @@
<template> <template>
<h1 class="title is-2">Profile</h1> <h1 class="title is-2">Profile</h1>
<form @submit.prevent="update"> <form @submit="update">
<div class="tile is-ancestor"> <div class="tile is-ancestor">
<div class="tile is-4 is-vertical is-parent"> <div class="tile is-4 is-vertical is-parent">
<div class="tile is-child box"> <div class="tile is-child box">
@ -8,7 +8,7 @@
<div class="field"> <div class="field">
<label class="label">Username</label> <label class="label">Username</label>
<div class="control"> <div class="control">
<input class="input" type="text" :value="user.username"> <input class="input" type="text" v-model="user.username">
</div> </div>
</div> </div>
<div class="field"> <div class="field">
@ -21,7 +21,7 @@
<div class="field"> <div class="field">
<label class="label">Email</label> <label class="label">Email</label>
<div class="control"> <div class="control">
<input class="input" type="email" :value="user.email"> <input class="input" type="email" v-model="user.email">
</div> </div>
</div> </div>
</div> </div>
@ -31,7 +31,7 @@
<p class="title">Change Password</p> <p class="title">Change Password</p>
<div class="field"> <div class="field">
<p class="control has-icons-left"> <p class="control has-icons-left">
<input class="input" type="password" placeholder="Current Password" v-model="currentPassword"> <input class="input" type="password" placeholder="Current Password" v-model="user.currentPassword">
<span class="icon is-small is-left"> <span class="icon is-small is-left">
<i class="fas fa-lock"></i> <i class="fas fa-lock"></i>
</span> </span>
@ -39,7 +39,7 @@
</div> </div>
<div class="field"> <div class="field">
<p class="control has-icons-left"> <p class="control has-icons-left">
<input class="input" type="password" placeholder="New Password" v-model="newPassword"> <input class="input" type="password" placeholder="New Password" v-model="user.newPassword">
<span class="icon is-small is-left"> <span class="icon is-small is-left">
<i class="fas fa-lock"></i> <i class="fas fa-lock"></i>
</span> </span>
@ -47,7 +47,7 @@
</div> </div>
<div class="field"> <div class="field">
<p class="control has-icons-left"> <p class="control has-icons-left">
<input class="input" type="password" placeholder="Confirm Password" v-model="repeatNewPassword"> <input class="input" type="password" placeholder="Confirm Password" v-model="user.repeatNewPassword">
<span class="icon is-small is-left"> <span class="icon is-small is-left">
<i class="fas fa-lock"></i> <i class="fas fa-lock"></i>
</span> </span>
@ -55,13 +55,16 @@
</div> </div>
<div class="field"> <div class="field">
<p class="control"> <p class="control">
<button class="button is-primary" type="submit"> <button class="button is-primary is-rounded" type="submit">
Update Update
</button> </button>
</p> </p>
</div> </div>
<div class="notification is-danger has-text-centered mt-5" v-if="msg"> <div class="notification is-danger has-text-centered mt-5" v-if="msgError">
<b>{{ msg }}</b> <b>{{ msgError }}</b>
</div>
<div class="notification is-success has-text-centered mt-5" v-else-if="msgSuccess">
<b>{{ msgSuccess }}</b>
</div> </div>
<div class="notification is-primary is-light mt-5" v-else> <div class="notification is-primary is-light mt-5" v-else>
<p>According to the traditional advicewhich is still gooda strong password:</p> <p>According to the traditional advicewhich is still gooda strong password:</p>
@ -82,38 +85,57 @@
import {Vue} from "vue-class-component"; import {Vue} from "vue-class-component";
interface User { interface User {
_id: string,
username: string, username: string,
registered: string email: string,
registered: string,
currentPassword: string,
newPassword: string,
repeatNewPassword: string,
} }
export default class Profile extends Vue { export default class Profile extends Vue {
private msg = ''; private msgSuccess = '';
private msgError = '';
private user!: User; private user!: User;
private date!: string; private date!: string;
private username = ''
private email = ''
private currentPassword = ''
private newPassword = ''
private repeatNewPassword = ''
created() { created() {
this.user = this.$store.getters.user this.user = this.$store.getters.user
this.date = new Date(this.user.registered).toLocaleString() this.date = new Date(this.user.registered).toLocaleString()
} }
private update() { private update() {
if (this.user.newPassword != this.user.repeatNewPassword) {
this.msgError = 'Passwords do not match!'
return
}
let user = { let user = {
username: this.username, _id: this.user._id,
email: this.email, username: this.user.username,
password: this.currentPassword, email: this.user.email,
newPassword: this.newPassword, password: this.user.currentPassword,
repeatNewPassword: this.repeatNewPassword newPassword: this.user.newPassword,
repeatNewPassword: this.user.repeatNewPassword
} }
this.$store.dispatch('updateUser', user) this.$store.dispatch('updateUser', user)
.then(() => this.$router.push({name: 'Profile', params: {username: this.username}})) .then(() => {
.catch((err: any) => this.msg = err.response.data.errors.message || err.message || 'Something went wrong!') this.msgSuccess = 'User updated!';
this.$router.push({
name: 'Profile',
params: {username: this.user.username}
})
})
.catch((err: any) => {
this.msgError = err.data.errors.message || err.message || 'Something went wrong!';
this.$router.push({
name: 'Profile',
params: {username: this.user.username}
})
}
)
} }
} }

Loading…
Cancel
Save