Browse Source

Fixed issues with POSTing. Issue with passportSocketIo not fixed.

theflametrooper 8 years ago
parent
commit
cf40c44723

+ 16 - 1
backend/app.js

@@ -14,6 +14,7 @@ const express          = require('express'),
       MongoStore       = require('connect-mongo')(session),
       bodyParser       = require('body-parser'),
       config           = require('config'),
+	  cookieParser	   = require('cookie-parser'),
       cors             = require('cors'),
       request          = require('request'),
       passport         = require('passport'),
@@ -64,15 +65,29 @@ function setupExpress() {
 		saveUninitialized: true
 	}));
 
+	app.use(passport.initialize());
+	app.use(passport.session());
+
+	passport.serializeUser((user, done) => {
+		done(null, user);
+	});
+
+	passport.deserializeUser((user, done) => {
+		done(null, user);
+	});
+
 	global.io.use(passportSocketIo.authorize({
+		passport: require('passport'),
 		cookieParser: require('cookie-parser'),
 		key: 'connect.sid',
 		secret: config.get('secret'),
 		store: mongoStore,
 		success: (data, accept) => {
+			console.log('success', data);
 			accept();
 		},
 		fail: (data, message, error, accept) => {
+			console.log('error', error, message);
 			accept();
 		}
 	}));
@@ -102,6 +117,6 @@ function setupExpress() {
 
 	app.use(cors());
 
-	socketHandler(coreHandler, global.io);
+	socketHandler(coreHandler, global.io, app);
 	expressHandler(coreHandler, app);
 }

+ 4 - 8
backend/logic/expressHandler.js

@@ -1,22 +1,18 @@
 'use strict';
 
 // npm modules
-const passport  = require('passport');
+const passport = require('passport');
 
 module.exports = (core, app) => {
 
-	app.post('/users/login', passport.authenticate('local'), function(req, res) {
+	app.post('/users/login', (req, res, next) => {
+		console.log('posted', req.user);
 		res.json(JSON.stringify(req.user));
 	});
 
-	app.post('/users/register', function(req, res) {
+	app.post('/users/register', (req, res) => {
 		core['/users/register'](req.body.username, req.body.email, req.body.password, req.body.recaptcha, result => {
 			res.send(JSON.stringify(result));
 		});
 	});
-
-	app.post('/users/logout', function(req, res) {
-		req.logout();
-		res.end();
-	});
 };

+ 10 - 1
backend/logic/socketHandler.js

@@ -1,6 +1,8 @@
 'use strict';
 
-module.exports = (core, io) => {
+const passport = require('passport');
+
+module.exports = (core, io, app) => {
 
 	io.on('connection', socket => {
 		console.log("User has connected");
@@ -13,6 +15,12 @@ module.exports = (core, io) => {
 			console.log('User has disconnected');
 		});
 
+		socket.on('/users/logout', () => {
+			app.use((req, res) => {
+				req.logout();
+			});
+		});
+
 		socket.on('error', err => {
 			console.log(err);
 		});
@@ -62,6 +70,7 @@ module.exports = (core, io) => {
 		});
 
 		// this lets the client socket know that they can start making request
+		console.log(socket.request.user)
 		socket.emit('ready', socket.request.user.logged_in);
 	});
 };

+ 24 - 40
frontend/App.vue

@@ -26,15 +26,8 @@
 		},
 		methods: {
 			logout() {
-				$.ajax({
-					method: "POST",
-					url: `${window.location.protocol + '//' + window.location.hostname + ':8081'}/users/logout`,
-					dataType: "json",
-					complete: msg => {
-						alert("Logged out!");
-						location.reload();
-					}
-				});
+				this.socket.emit('/users/logout');
+				location.reload();
 			}
 		},
 		ready: function() {
@@ -50,45 +43,36 @@
 		},
 		events: {
 			'register': function() {
-				$.ajax({
-					method: "POST",
-					url: `${window.location.protocol + '//' + window.location.hostname + ':8081'}/users/register`,
-					data: JSON.stringify({
+				fetch(`${window.location.protocol + '//' + window.location.hostname + ':8081'}/users/register`, {
+					method: 'POST',
+					headers: {
+						'Accept': 'application/json',
+						'Content-Type': 'application/json; charset=utf-8'
+					},
+					body: JSON.stringify({
 						email: this.register.email,
 						username: this.register.username,
 						password: this.register.password,
 						recaptcha: grecaptcha.getResponse()
-					}),
-					contentType: "application/json; charset=utf-8",
-					dataType: "json",
-					success: function (msg) {
-						if (msg) console.log(msg);
-					},
-					error: function (err) {
-						if (err) console.log(err);
-						alert("Not registered!");
-					}
-				});
+					})
+				}).then(response => {
+					alert('Now sign in!');
+				})
 			},
 			'login': function() {
-				$.ajax({
-					method: "POST",
-					url: `${window.location.protocol + '//' + window.location.hostname + ':8081'}/users/login`,
-					data: JSON.stringify({
+				fetch(`${window.location.protocol + '//' + window.location.hostname + ':8081'}/users/login`, {
+					method: 'POST',
+					headers: {
+						'Accept': 'application/json',
+						'Content-Type': 'application/json; charset=utf-8'
+					},
+					body: JSON.stringify({
 						email: this.login.email,
 						password: this.login.password
-					}),
-					contentType: "application/json; charset=utf-8",
-					dataType: "json",
-					success: function (msg) {
-						if (msg) console.log(msg);
-						location.reload();
-						//do something
-					},
-					error: function (err) {
-						if (err) console.log(err);
-						alert("Not logged in!");
-					}
+					})
+				}).then(response => {
+					console.log(response);
+					// location.reload();
 				});
 			},
 			'joinStation': function(id) {

+ 1 - 1
frontend/components/pages/Home.vue

@@ -115,7 +115,7 @@
 <style lang="scss">
 
 	@import 'theme.scss';
-	
+
 	* { box-sizing: border-box; font-family: Roboto, sans-serif; }
 
 	html {

+ 1 - 0
frontend/package.json

@@ -28,6 +28,7 @@
     "vue-html-loader": "^1.2.3",
     "vue-loader": "^8.5.2",
     "vue-style-loader": "^1.0.0",
+	"whatwg-fetch": "^0.11.1",
     "webpack": "^1.13.2",
     "webpack-dev-server": "^1.15.1"
   },