فهرست منبع

Fixed logout not removing userSession.

KrisVos130 8 سال پیش
والد
کامیت
7a0d9d9713
3فایلهای تغییر یافته به همراه24 افزوده شده و 12 حذف شده
  1. 14 5
      backend/logic/actions/users.js
  2. 2 4
      backend/logic/io.js
  3. 8 3
      frontend/App.vue

+ 14 - 5
backend/logic/actions/users.js

@@ -142,14 +142,23 @@ module.exports = {
 
 	},
 
-	logout: (session, cb) => {
+	logout: (sessionId, cb) => {
 
-		if (!session) return cb({ status: 'failure', message: `You're not currently logged in` });
+		cache.hget('sessions', sessionId, (err, session) => {
+			if (err || !session) return cb({ 'status': 'failure', message: 'Something went wrong while logging you out.' });
+			if (!session.userSessionId) return cb({ 'status': 'failure', message: 'You are not logged in.' });
 
-		//TODO Remove session
-		session = null;
+			cache.hget('userSessions', session.userSessionId, (err, userSession) => {
+				if (err || !userSession) return cb({ 'status': 'failure', message: 'Something went wrong while logging you out.' });
+				if (!userSession) return cb({ 'status': 'failure', message: 'You are not logged in.' });
+
+				cache.hdel('userSessions', session.userSessionId, (err) => {
+					if (err || !userSession) return cb({ 'status': 'failure', message: 'Something went wrong while logging you out.' });
+					return cb({ 'status': 'success', message: 'You have been successfully logged out.' });
+				});
+			});
+		});
 
-		return cb({ status: 'success', message: `You've been successfully logged out` });
 	},
 
 	findByUsername: (session, username, cb) => {

+ 2 - 4
backend/logic/io.js

@@ -69,7 +69,7 @@ module.exports = {
 						// load the session from the cache
 						cache.hget('sessions', socket.sessionId, (err, session) => {
 							if (err && err !== true) {
-								return cb({
+								if (typeof cb === 'function') return cb({
 									status: 'error',
 									message: 'An error occurred while obtaining your session'
 								});
@@ -81,10 +81,8 @@ module.exports = {
 							// call the action, passing it the session, and the arguments socket.io passed us
 							actions[namespace][action].apply(null, [socket.sessionId].concat(args).concat([
 								(result) => {
-									// store the session id
-									//if (name == 'users.login' && result.user) socket.sessionId = result.user.sessionId;
 									// respond to the socket with our message
-									cb(result);
+									if (typeof cb === 'function') return cb(result);
 								}
 							]));
 						});

+ 8 - 3
frontend/App.vue

@@ -35,9 +35,14 @@
 		},
 		methods: {
 			logout: function () {
-				this.socket.emit('users.logout');
-				document.cookie = 'SID=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
-				location.reload();
+				this.socket.emit('users.logout', (result) => {
+					if (result.status === 'success') {
+						document.cookie = 'SID=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
+						location.reload();
+					} else {
+						Toast.methods.addToast(result.message, 4000);
+					}
+				});
 			}
 		},
 		ready() {