Browse Source

Added auth.

KrisVos130 8 years ago
parent
commit
09add816b5

+ 5 - 3
backend/logic/actions/stations.js

@@ -98,7 +98,7 @@ module.exports = {
 	 * Joins the station by its id
 	 *
 	 * @param session
-	 * @param stationId - the station join
+	 * @param stationId - the station id
 	 * @param cb
 	 * @return {{ status: String, userCount: Integer }}
 	 */
@@ -128,10 +128,11 @@ module.exports = {
 	 * Skips the users current station
 	 *
 	 * @param session
+	 * @param stationId - the station id
 	 * @param cb
 	 * @return {{ status: String, skipCount: Integer }}
 	 */
-	skip: (session, cb) => {
+	skip: (session, stationId, cb) => {
 
 		if (!session) return cb({ status: 'failure', message: 'You must be logged in to skip a song!' });
 
@@ -163,10 +164,11 @@ module.exports = {
 	 * Leaves the users current station
 	 *
 	 * @param session
+	 * @param stationId - the station id
 	 * @param cb
 	 * @return {{ status: String, userCount: Integer }}
 	 */
-	leave: (session, cb) => {
+	leave: (session, stationId, cb) => {
 		initializeAndReturnStation(stationId, (err, station) => {
 
 			if (err && err !== true) {

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

@@ -32,9 +32,10 @@ module.exports = {
 					if (match) {
 
 						// store the session in the cache
-						cache.hset('sessions', utils.guid(), cache.schemas.session());
+						let sessionId = utils.guid();
+						cache.hset('sessions', sessionId, cache.schemas.session());
 
-						next(null, { status: 'failure', message: 'Login successful', user });
+						next(null, { status: 'success', message: 'Login successful', user, sessionId: sessionId });
 					}
 					else {
 						next(null, { status: 'failure', message: 'User not found' });
@@ -143,6 +144,7 @@ module.exports = {
 
 		if (!session) return cb({ status: 'failure', message: `You're not currently logged in` });
 
+		//TODO Remove session
 		session = null;
 
 		cb({ status: 'success', message: `You've been successfully logged out` });

+ 20 - 4
backend/logic/io.js

@@ -5,6 +5,7 @@
 const app = require('./app');
 const actions = require('./actions');
 const cache = require('./cache');
+const utils = require('./utils');
 
 module.exports = {
 
@@ -14,6 +15,13 @@ module.exports = {
 
 		this.io = require('socket.io')(app.server);
 
+		this.io.use(function(socket, next){
+			let cookies = socket.request.headers.cookie;
+			// set the sessionId for the socket (this will have to be checked every request, this allows us to have a logout all devices option)
+			socket.sessionId = utils.cookies.parseCookies(cookies).SID;
+			return next();
+		});
+
 		this.io.on('connection', socket => {
 
 			console.log("io: User has connected");
@@ -21,9 +29,9 @@ module.exports = {
 			// catch when the socket has been disconnected
 			socket.on('disconnect', () => {
 
-				// remove the user from their current station
+				// remove the user from their current station (if any)
 				if (socket.sessionId) {
-					actions.stations.leave(socket.sessionId, result => {});
+					//actions.stations.leave(socket.sessionId, result => {});
 					delete socket.sessionId;
 				}
 
@@ -48,7 +56,6 @@ module.exports = {
 
 						// load the session from the cache
 						cache.hget('sessions', socket.sessionId, (err, session) => {
-
 							if (err && err !== true) {
 								return cb({
 									status: 'error',
@@ -73,7 +80,16 @@ module.exports = {
 				})
 			});
 
-			socket.emit('ready');
+			//TODO check if session is valid before returning true/false
+			cache.hget('sessions', socket.sessionId, (err, session) => {
+				if (err && err !== true) {
+					socket.emit('ready', false);
+				} else if (session) {
+					socket.emit('ready', true);
+				} else {
+					socket.emit('ready', false);
+				}
+			});
 		});
 
 		cb();

+ 22 - 1
backend/logic/utils.js

@@ -106,5 +106,26 @@ module.exports = {
 	getRandomNumber: (min, max) => Math.floor(Math.random() * (max - min + 1)) + min,
 	convertTime,
 	Timer,
-	guid: () => [1,1,0,1,0,1,0,1,0,1,1,1].map(b => b ? Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1) : '-').join('')
+	guid: () => [1,1,0,1,0,1,0,1,0,1,1,1].map(b => b ? Math.floor((1 + Math.random()) * 0x10000).toString(16).substring(1) : '-').join(''),
+	cookies: {
+		parseCookies: cookieString => {
+			let cookies = {};
+			cookieString.split("; ").map((cookie) => {
+				(cookies[cookie.substring(0, cookie.indexOf("="))] = cookie.substring(cookie.indexOf("=") + 1, cookie.length));
+			});
+			return cookies;
+		},
+		toString: cookies => {
+			let newCookie = [];
+			for (let prop in cookie) {
+				newCookie.push(prop + "=" + cookie[prop]);
+			}
+			return newCookie.join("; ");
+		},
+		removeCookie: (cookieString, cookieName) => {
+			var cookies = this.parseCookies(cookieString);
+			delete cookies[cookieName];
+			return this.toString(cookies);
+		}
+	}
 };

+ 9 - 1
frontend/App.vue

@@ -27,6 +27,7 @@
 		methods: {
 			logout() {
 				this.socket.emit('users.logout');
+				document.cookie = 'SID=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
 				location.reload();
 			}
 		},
@@ -53,7 +54,14 @@
 
 				this.socket.emit('users.login', email, password, (result) => {
 					console.log(result);
-					location.reload();
+					if (result.status === 'success') {
+						let date = new Date();
+						date.setTime(new Date().getTime() + (2*365*24*60*60*1000));
+						document.cookie = "SID=" + result.sessionId + "; expires="+ date.toGMTString() +"; path=/";
+						location.reload();
+					} else {
+						//TODO Error toast
+					}
 				});
 			},
 			'joinStation': function (id) {