Browse Source

Continued working on Settings. Added backend function and most of frontend functions

theflametrooper 8 years ago
parent
commit
966d030389
2 changed files with 94 additions and 4 deletions
  1. 36 2
      backend/logic/actions/users.js
  2. 58 2
      frontend/components/User/Settings.vue

+ 36 - 2
backend/logic/actions/users.js

@@ -140,7 +140,7 @@ module.exports = {
 		//TODO Remove session
 		session = null;
 
-		cb({ status: 'success', message: `You've been successfully logged out` });
+		return cb({ status: 'success', message: `You've been successfully logged out` });
 	},
 
 	findByUsername: (session, username, cb) => {
@@ -165,6 +165,40 @@ module.exports = {
 				});
 			}
 		});
-	}
+	},
+
+	findBySession: (session, cb) => {
+		return cb({
+			status: 'success',
+			data: session
+		});
+	},
+
+	update: (session, user_id, property, value, cb) => {
+        db.models.user.findOne({ _id: user_id }, (err, user) => {
+            if (err) throw err;
+            else if (!user) cb({ status: 'error', message: 'Invalid User ID' });
+            else if (user[property] && user[property] !== value) {
+                if (property == 'services.password.password') {
+                    bcrypt.compare(user[property], value, (err, res) => {
+                        if (err) throw err;
+                        bcrypt.genSalt(10, (err, salt) => {
+                            if (err) throw err;
+                            bcrypt.hash(value, salt, (err, hash) => {
+                                if (err) throw err;
+                                user[property] = hash;
+                            });
+                        });
+                    });
+                } else user[property] = value;
+                user.save(err => {
+                    if (err) cb({ status: 'error', message: err.message });
+                });
+            } else {
+                cb({ status: 'error', message: 'Field has not changed' });
+            }
+        });
+    },
+
 
 };

+ 58 - 2
frontend/components/User/Settings.vue

@@ -19,7 +19,7 @@
 		<label class="label">Change Password</label>
 		<div class="control is-grouped">
 			<p class="control is-expanded has-icon has-icon-right">
-				<input class="input is-danger" type="text" placeholder="Enter current password">
+				<input class="input is-danger" type="text" placeholder="Enter current password" v-model="currentPassword">
 				<!-- Check if correct -->
 				<i class="fa fa-warning"></i>
 				<span class="help is-danger">This password is invalid</span>
@@ -39,12 +39,15 @@
 </template>
 
 <script>
+	import { Toast } from 'vue-roaster';
+
 	import MainHeader from '../MainHeader.vue';
 	import MainFooter from '../MainFooter.vue';
 
 	export default {
 		data() {
 			return {
+				currentPassword: '',
 				user: {}
 			}
 		},
@@ -58,11 +61,64 @@
 						if (res.status == 'error') console.error(res.message); // Add 404/ Not found Component with link back to home, for now just console.log
 						else _this.user = res.data;
 					});
+					_this.socket.emit('users.findBySession', res => {
+						console.log(res, location.href)
+						/* if (res.data == null) */ console.log(location.href);
+					});
 					clearInterval(socketInterval);
 				}
 			}, 100);
 		},
-		components: { MainHeader, MainFooter },
+		methods: {
+			changePassword: function () {
+				if (this.currentPassword !== "" && this.user.password !== "") {
+					// need to refactor to send whoever is currently logged in
+					socket.emit('users.update', '582a0724023ecc015c0bef42', 'services.password.password', this.user.password, res => {
+						if (res.status == 'error') Toast.methods.addToast(res.message, 2000);
+						else Toast.methods.addToast('Successfully changed password', 2000);
+					});
+				} else {
+					Toast.methods.addToast('Current password field is incorrect', 2000);
+				}
+			},
+			changeEmail: function () {
+				if (this.user.email !== "") {
+					socket.emit('users.update', '582a0724023ecc015c0bef42', 'email.address', this.user.email, res => {
+						if (res.status == 'error') Toast.methods.addToast(res.message, 2000);
+						else Toast.methods.addToast('Successfully changed email address', 2000);
+					});
+				} else {
+					Toast.methods.addToast('Field cannot be empty', 2000);
+				}
+			},
+			// Will be added shortly:
+			// changeAvatar() {
+			//     let files = document.getElementById("avatar").files;
+			//     if (files === undefined || files === null) return Materialize.toast(this.getFromLang("error.profilePictureNotSupported"), 4000);
+			//     if (files.length !== 1) return Materialize.toast(this.getFromLang("error.profilePictureOnlyOneFileAllowed"), 4000);
+			//     if (files[0].size >= 2097152) return Materialize.toast(this.getFromLang("error.tooBigProfileImage"), 4000);
+			//     if (files[0].type.indexOf("image/") == -1) return Materialize.toast(this.getFromLang("error.notAnImage"), 4000);
+			//     let local = this;
+			//     fetch(window.location.protocol + '//' + window.location.hostname + ':8081' + '/auth/update/avatar', {
+			//         method: 'POST',
+			//         body: new FormData($('#updateAvatar')[0])
+			//     }).then(response => {
+			//         return response.json()
+			//     }).then(body => {
+			//         if (body.status == 'error') Materialize.toast(body.message, 4000);
+			//         else Materialize.toast(local.getFromLang("settings.profilePictureUpdated"), 4000);
+			//         console.log(body);
+			//     });
+			// },
+			changeUsername: function () {
+				if (this.user.username == "") return Toast.methods.addToast('Field cannot be empty', 2000);
+				socket.emit('users.update', '582a0724023ecc015c0bef42', 'username', this.user.username, res => {
+					if (res.status == 'error') Toast.methods.addToast(res.message, 2000);
+					else Toast.methods.addToast('Successfully changed username', 2000);
+				});
+			}
+		},
+		components: { MainHeader, MainFooter }
 	}
 </script>