瀏覽代碼

Added settings live password/github updating.

KrisVos130 8 年之前
父節點
當前提交
553a5543c1
共有 3 個文件被更改,包括 69 次插入11 次删除
  1. 40 1
      backend/logic/actions/users.js
  2. 1 0
      backend/logic/app.js
  3. 28 10
      frontend/components/User/Settings.vue

+ 40 - 1
backend/logic/actions/users.js

@@ -21,6 +21,42 @@ cache.sub('user.updateUsername', user => {
 	});
 });
 
+cache.sub('user.linkPassword', userId => {
+	console.log("LINK4", userId);
+	utils.socketsFromUser(userId, sockets => {
+		sockets.forEach(socket => {
+			socket.emit('event:user.linkPassword');
+		});
+	});
+});
+
+cache.sub('user.linkGitHub', userId => {
+	console.log("LINK1", userId);
+	utils.socketsFromUser(userId, sockets => {
+		sockets.forEach(socket => {
+			socket.emit('event:user.linkGitHub');
+		});
+	});
+});
+
+cache.sub('user.unlinkPassword', userId => {
+	console.log("LINK2", userId);
+	utils.socketsFromUser(userId, sockets => {
+		sockets.forEach(socket => {
+			socket.emit('event:user.unlinkPassword');
+		});
+	});
+});
+
+cache.sub('user.unlinkGitHub', userId => {
+	console.log("LINK3", userId);
+	utils.socketsFromUser(userId, sockets => {
+		sockets.forEach(socket => {
+			socket.emit('event:user.unlinkGitHub');
+		});
+	});
+});
+
 module.exports = {
 
 	/**
@@ -643,7 +679,7 @@ module.exports = {
 	 * @param {Function} cb - gets called with the result
 	 * @param {String} userId - the userId automatically added by hooks
 	 */
-	changePasswordWithCode: hooks.loginRequired((session, code, newPassword, cb) => {
+	changePasswordWithCode: hooks.loginRequired((session, code, newPassword, cb, userId) => {
 		async.waterfall([
 			(next) => {
 				if (!code || typeof code !== 'string') return next('Invalid code1.');
@@ -677,6 +713,7 @@ module.exports = {
 				cb({status: 'failure', message: error});
 			} else {
 				logger.success("ADD_PASSWORD_WITH_CODE", `Code '${code}' successfully added password.`);
+				cache.pub('user.linkPassword', userId);
 				cb({
 					status: 'success',
 					message: 'Successfully added password.'
@@ -712,6 +749,7 @@ module.exports = {
 				cb({status: 'failure', message: error});
 			} else {
 				logger.success("UNLINK_PASSWORD", `Unlinking password successful for userId '${userId}'.`);
+				cache.pub('user.unlinkPassword', userId);
 				cb({
 					status: 'success',
 					message: 'Successfully unlinked password.'
@@ -747,6 +785,7 @@ module.exports = {
 				cb({status: 'failure', message: error});
 			} else {
 				logger.success("UNLINK_GITHUB", `Unlinking GitHub successful for userId '${userId}'.`);
+				cache.pub('user.unlinkGitHub', userId);
 				cb({
 					status: 'success',
 					message: 'Successfully unlinked GitHub.'

+ 1 - 0
backend/logic/app.js

@@ -93,6 +93,7 @@ const lib = {
 								if (user.services.github && user.services.github.id) return redirectOnErr(res, 'Account already has GitHub linked.');
 								db.models.user.update({_id: user._id}, {$set: {"services.github": {id: body.id, access_token}}}, (err) => {
 									if (err) return redirectOnErr(res, err.message);
+									cache.pub('user.linkGitHub', user._id);
 									res.redirect(`${config.get('domain')}/settings`);
 								});
 							});

+ 28 - 10
frontend/components/User/Settings.vue

@@ -22,8 +22,8 @@
 				<button class="button is-success" @click="changeEmail()">Save Changes</button>
 			</p>
 		</div>
-		<label class="label" v-if="user.password">Change Password</label>
-		<div class="control is-grouped" v-if="user.password">
+		<label class="label" v-if="password">Change Password</label>
+		<div class="control is-grouped" v-if="password">
 			<p class="control is-expanded has-icon has-icon-right">
 				<input class="input" type="password" placeholder="Change password" v-model="newPassword">
 			</p>
@@ -33,8 +33,8 @@
 		</div>
 
 
-		<label class="label" v-if="!user.password">Add password</label>
-		<div class="control is-grouped" v-if="!user.password">
+		<label class="label" v-if="!password">Add password</label>
+		<div class="control is-grouped" v-if="!password">
 			<button class="button is-success" @click="requestPassword()" v-if="passwordStep === 1">Request password email</button><br>
 
 
@@ -53,18 +53,18 @@
 				<button class="button is-success" @click="setPassword()">Set password</button>
 			</p>
 		</div>
-		<a href="#" v-if="passwordStep === 1 && !user.password" @click="passwordStep = 2">Skip this step</a>
+		<a href="#" v-if="passwordStep === 1 && !password" @click="passwordStep = 2">Skip this step</a>
 
 
-		<a class="button is-github" v-if="!user.github" :href='$parent.serverDomain + "/auth/github/link"'>
+		<a class="button is-github" v-if="!github" :href='$parent.serverDomain + "/auth/github/link"'>
 			<div class='icon'>
 				<img class='invert' src='/assets/social/github.svg'/>
 			</div>
 			&nbsp; Link GitHub to account
 		</a>
 
-		<button class="button is-danger" @click="unlinkPassword()" v-if="user.password && user.github">Remove logging in with password</button>
-		<button class="button is-danger" @click="unlinkGitHub()" v-if="user.password && user.github">Remove logging in with GitHub</button>
+		<button class="button is-danger" @click="unlinkPassword()" v-if="password && github">Remove logging in with password</button>
+		<button class="button is-danger" @click="unlinkGitHub()" v-if="password && github">Remove logging in with GitHub</button>
 	</div>
 	<main-footer></main-footer>
 </template>
@@ -83,6 +83,8 @@
 			return {
 				user: {},
 				newPassword: '',
+				password: false,
+				github: false,
 				setNewPassword: '',
 				passwordStep: 1,
 				passwordCode: ''
@@ -90,10 +92,14 @@
 		},
 		ready: function() {
 			let _this = this;
-			io.getSocket((socket) => {
+			io.getSocket(socket => {
 				_this.socket = socket;
 				_this.socket.emit('users.findBySession', res => {
-					if (res.status == 'success') { _this.user = res.data; } else {
+					if (res.status == 'success') {
+						_this.user = res.data; 
+						_this.password = _this.user.password;
+						_this.github = _this.user.github;
+					} else {
 						_this.$parent.isLoginActive = true;
 						Toast.methods.addToast('Your are currently not signed in', 3000);
 					}
@@ -101,6 +107,18 @@
 				_this.socket.on('event:user.username.changed', username => {
 					_this.$parent.username = username;
 				});
+				_this.socket.on('event:user.linkPassword', () => {console.log(1);
+					_this.password = true;
+				});
+				_this.socket.on('event:user.linkGitHub', () => {console.log(2);
+					_this.github = true;
+				});
+				_this.socket.on('event:user.unlinkPassword', () => {console.log(3);
+					_this.password = false;
+				});
+				_this.socket.on('event:user.unlinkGitHub', () => {console.log(4);
+					_this.github = false;
+				});
 			});
 		},
 		methods: {