Browse Source

Fixed a lot of @luveti mistakes ;)

theflametrooper 8 years ago
parent
commit
02693d3500

+ 1 - 1
backend/app.js

@@ -86,7 +86,7 @@ globals.db.connection.once('open', _ => {
 
 	passport.use(new LocalStrategy({ usernameField: 'email' }, (email, password, done) => {
 		process.nextTick(() => {
-			globals.db.user.findOne({ "email.address": email }, (err, user) => {
+			globals.db.models.user.findOne({ "email.address": email }, (err, user) => {
 				if (err) return done(err);
 				if (!user) return done(null, false);
 				bcrypt.compare(password, user.services.password.password, function(err, res) {

+ 2 - 1
backend/config/template.json

@@ -1,6 +1,6 @@
 {
 	"secret": "",
-	"domain": "http://dev.musare.com",
+	"domain": "",
 	"apis": {
 		"youtube": {
 			"key": ""
@@ -20,6 +20,7 @@
 	"cors": {
 		"origin": [
 			"http://localhost:8080",
+			"http://192.168.99.100:8080",
 			"http://dev.musare.com"
 		]
 	}

+ 1 - 55
backend/logic/coreHandler.js

@@ -54,46 +54,6 @@ module.exports = {
 
 	// core route handlers
 
-	'/users/login': (session, identifier, password, cb) => {
-
-		waterfall([
-
-			// check if a user with the requested identifier exists
-			(next) => globals.db.models.user.findOne({
-				$or: [{ 'username': identifier }, { 'email.address': identifier }]
-			}, next),
-
-			// if the user doesn't exist, respond with a failure
-			// otherwise compare the requested password and the actual users password
-			(user, next) => {
-				if (!user) return next(true, { status: 'failure', message: 'User not found' });
-				bcrypt.compare(password, user.services.password.password, next);
-			},
-
-			// if the user exists, and the passwords match, respond with a success
-			(result, next) => {
-
-				// TODO: Authenticate the user with Passport here I think?
-				// TODO: We need to figure out how other login methods will work
-
-				next(null, {
-					status: result ? 'success': 'failure',
-					message: result ? 'Logged in' : 'User not found'
-				});
-			}
-
-		], (err, payload) => {
-			// log this error somewhere
-			if (err && err !== true) {
-				console.error(err);
-				return cb({ status: 'error', message: 'An error occurred while logging in' });
-			}
-			// respond with the payload that was passed to us earlier
-			cb(payload);
-		});
-
-	},
-
 	'/users/register': (session, username, email, password, recaptcha, cb) => {
 
 		waterfall([
@@ -235,14 +195,7 @@ module.exports = {
 				return cb({ status: 'error', message: 'Failed to search youtube with the requested query' });
 			}
 
-			try {
-				let json = JSON.parse(body);
-			}
-			catch (e) {
-				return cb({ status: 'error', message: 'Invalid response from youtube' });
-			}
-
-			cb({ status: 'success', data: json });
+			cb({ status: 'success', data: JSON.parse(body) });
 		});
 	},
 
@@ -265,13 +218,6 @@ module.exports = {
 				return cb({ status: 'error', message: 'Failed to find song from youtube' });
 			}
 
-			try {
-				let json = JSON.parse(body);
-			}
-			catch (e) {
-				return cb({ status: 'error', message: 'Invalid response from youtube' });
-			}
-
 			const newSong = new globals.db.models.song({
 				id: json.items[0].id,
 				title: json.items[0].snippet.title,

+ 5 - 4
backend/logic/expressHandler.js

@@ -1,11 +1,12 @@
 'use strict';
 
+const passport = require('passport');
+
 module.exports = (core, app) => {
 
-	app.post('/users/login', (req, res) => {
-		core['/users/login'](req.user, req.body.identifier, req.body.password, result => {
-			res.end(JSON.stringify(result));
-		});
+	app.post('/users/login', passport.authenticate('local'), (req, res) => {
+		console.log('posted', req.user);
+  		res.json(JSON.stringify(req.user));
 	});
 
 	app.post('/users/register', (req, res) => {

+ 7 - 6
backend/logic/stations.js

@@ -22,7 +22,7 @@ module.exports = {
 
 				socket.emit("connected", {
 					currentSong: this.currentSong,
-					startedAt: this.currentSong.startedAt,
+					startedAt: this.startedAt,
 					paused: this.paused,
 					timePaused: this.timePaused,
 					currentTime: Date.now()
@@ -52,9 +52,10 @@ module.exports = {
 
 				local.timePaused = 0;
 				local.timer = undefined;
-				local.currentSong = local.playlist[this.currentSongIndex];
-
-				local.nextSong();
+				if (local.playlist[this.currentSongIndex]) {
+					local.currentSong = local.playlist[this.currentSongIndex];
+					local.nextSong();
+				}
 			});
 		}
 
@@ -76,8 +77,8 @@ module.exports = {
 				}, this.currentSong.duration, this.paused);
 
 				this.timePaused = 0;
-				this.currentSong.startedAt = Date.now();
-				this.nsp.emit("nextSong", this.currentSong, this.currentSong.startedAt);
+				this.startedAt = Date.now();
+				this.nsp.emit("nextSong", this.currentSong, this.startedAt);
 			}
 		}
 

+ 1 - 0
frontend/App.vue

@@ -33,6 +33,7 @@
 		ready: function() {
 			let local = this;
 			local.socket = io(window.location.protocol + '//' + window.location.hostname + ':8081');
+
 			local.socket.on("ready", status => {
 				local.loggedIn = status;
 			});

+ 6 - 5
frontend/components/pages/Station.vue

@@ -218,14 +218,15 @@
 			},
 			submitQuery: function() {
 				let local = this;
-				local.socket.emit("/youtube/getVideo/:query", local.querySearch, function(data) {
+				local.socket.emit("/youtube/getVideo/:query", local.querySearch, function(results) {
+					results = results.data;
 					local.queryResults = [];
-					for (let i = 0; i < data.items.length; i++) {
+					for (let i = 0; i < results.items.length; i++) {
 						local.queryResults.push({
-							id: data.items[i].id.videoId,
+							id: results.items[i].id.videoId,
 							url: `https://www.youtube.com/watch?v=${this.id}`,
-							title: data.items[i].snippet.title,
-							thumbnail: data.items[i].snippet.thumbnails.default.url
+							title: results.items[i].snippet.title,
+							thumbnail: results.items[i].snippet.thumbnails.default.url
 						});
 					}
 				});