Browse Source

Fixed and simplified Authentication

theflametrooper 8 years ago
parent
commit
6a2a510a87

+ 1 - 76
backend/app.js

@@ -75,9 +75,7 @@ function setupExpress() {
 			accept();
 		},
 		fail: function (data, message, error, accept) {
-			console.log(message);
-			if (error && message !== "Passport was not initialized")
-				throw new Error(message);
+			if (error) throw new Error(message);
 			accept();
 		}
 	}));
@@ -94,9 +92,7 @@ function setupExpress() {
 	});
 
 	passport.use(new LocalStrategy({usernameField: 'email'}, (email, password, done) => {
-		console.log(email, password);
 		process.nextTick(() => {
-			console.log(email, password);
 			global.db.user.findOne({"email.address": email}, (err, user) => {
 				if (err) return done(err);
 				if (!user) return done(null, false);
@@ -113,77 +109,6 @@ function setupExpress() {
 		});
 	}));
 
-	passport.use(new GitHubStrategy({
-			clientID: config.get("apis.github.client"),
-			clientSecret: config.get("apis.github.secret"),
-			callbackURL: `${config.get("domain")}/users/github/callback`
-		},
-		function(accessToken, refreshToken, profile, done) {
-			console.log('new github auth', accessToken, refreshToken, profile);
-			/*User.findOrCreate({ githubId: profile.id }, function (err, user) {
-				return cb(err, user);
-			});*/
-			global.db.user.findOne({"services.github.id": profile._json.id}, (err, id) => {
-				if (err) return done(err);
-				if (!id) {
-					global.db.user.findOne({"username": profile.username}, (err, username) => {
-						if (err) return done(err);
-						if (!username) {
-							let newUser = new global.db.user({
-								username: profile.username,
-								services: {
-									discord: {
-										id: profile.id
-									}
-								}
-							});
-							newUser.save(err => {
-								if (err) throw err;
-								return done(null, newUser);
-							});
-						} else {
-							return done(null, user);
-						}
-					});
-				}
-			});
-		}
-	));
-
-	passport.use(new DiscordStrategy({
-			clientID: config.get("apis.discord.client"),
-			clientSecret: config.get("apis.discord.secret"),
-			callbackURL: `${config.get("domain")}/users/discord/callback`
-		},
-		function(accessToken, refreshToken, profile, done) {
-			console.log('new discord auth', accessToken, refreshToken, profile);
-			global.db.user.findOne({"services.discord.id": profile.id}, (err, id) => {
-				if (err) return done(err);
-				if (!id) {
-					global.db.user.findOne({"username": profile.username}, (err, username) => {
-						if (err) return done(err);
-						if (!username) {
-							let newUser = new global.db.user({
-								username: profile.username,
-								services: {
-									discord: {
-										id: profile.id
-									}
-								}
-							});
-							newUser.save(err => {
-								if (err) throw err;
-								return done(null, newUser);
-							});
-						} else {
-							return done(null, user);
-						}
-					});
-				}
-			});
-		}
-	));
-
 	app.use(bodyParser.json());
 	app.use(bodyParser.urlencoded({
 		extended: true

+ 1 - 2
backend/logic/coreHandler.js

@@ -79,7 +79,6 @@ module.exports = {
 				'response': recaptcha
 			}
 		}, function (error, response, body) {
-			console.log(error, body, error === null, JSON.parse(body).success === true);
 			if (error === null && JSON.parse(body).success === true) {
 				body = JSON.parse(body);
 				global.db.user.findOne({'username': username}, function (err, user) {
@@ -92,7 +91,7 @@ module.exports = {
 							if (err) return cb(err);
 							if (user) return cb("email");
 							else {
-								//TODO Email verification code, send email
+								// TODO: Email verification code, send email
 								bcrypt.genSalt(10, function (err, salt) {
 									if (err) {
 										return cb(err);

+ 2 - 15
backend/logic/expressHandler.js

@@ -6,12 +6,11 @@ const passport  = require('passport');
 module.exports = (core, app) => {
 
 	app.post('/users/login', passport.authenticate('local'), function(req, res) {
-		console.log("Test136", req.user);
+		console.log("Logged in:", req.user);
 		res.json(JSON.stringify(req.user));
-		//res.end();
 	});
 
-	app.get('/users/logout', function(req, res) {
+	app.post('/users/logout', function(req, res) {
 		req.logout();
 		res.end();
 	});
@@ -22,18 +21,6 @@ module.exports = (core, app) => {
 		});
 	});
 
-	app.get('/users/github', passport.authenticate('github'));
-
-	app.get('/users/github/callback', passport.authenticate('github'), function (req, res) {
-		res.redirect('/');
-	});
-
-	app.get('/users/discord', passport.authenticate('discord', {scope: ['identify', 'email']}));
-
-	app.get('/users/discord/callback', passport.authenticate('discord'), function (req, res) {
-		res.redirect('/');
-	});
-
 	app.get('/stations', (req, res) => {
 		core['/stations'](result => {
 			res.send(JSON.stringify(result));

+ 1 - 7
backend/logic/socketHandler.js

@@ -10,12 +10,6 @@ module.exports = (core, io) => {
 			console.log('User has disconnected');
 		});
 
-		socket.on('/users/register', (username, email, password, recaptcha, cb) => {
-			core['/users/register'](result => {
-				cb(result);
-			});
-		});
-
 		socket.on('/stations', cb => {
 			core['/stations'](result => {
 				cb(result);
@@ -61,6 +55,6 @@ module.exports = (core, io) => {
 		});*/
 
 		// this lets the client socket know that they can start making request
-		//socket.emit('ready', user.logged_in);
+		socket.emit('ready', socket.request.user.logged_in);
 	});
 };

+ 1 - 1
frontend/build/index.html

@@ -4,7 +4,7 @@
 	<meta charset="UTF-8">
 	<meta name="viewport" content="width=device-width, initial-scale=1">
 	<title>Musare</title>
-	<link rel="shortcut icon" type="image/x-icon" href="https://musare.com/favicon.ico" />
+	<!--<link rel="shortcut icon" type="image/x-icon" href="https://musare.com/favicon.ico" />-->
 	<link href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.6.3/css/font-awesome.min.css" rel="stylesheet" type="text/css">
 	<link href="https://fonts.googleapis.com/css?family=Roboto:100,400" rel="stylesheet">
 	<link href="https://fonts.googleapis.com/icon?family=Material+Icons" rel="stylesheet">

+ 17 - 5
frontend/components/MainHeader.vue

@@ -15,9 +15,11 @@
 					<li><a v-link="{ path: '/admin/queue' }">Admin Queue</a></li>
 					<li><a href="#">The Project</a></li>
 					<li><a href="#">Donate</a></li>
-					<li><a href="#" @click="$parent.logout()">Logout</a></li>
-					<li><a href="#" data-toggle="modal" data-target="#register">Register</a></li>
-					<li><a href="#" data-toggle="modal" data-target="#login">Login</a></li>
+					<li v-if="$parent.$parent.loggedIn"><a href="#" @click="$parent.$parent.logout()">Logout</a></li>
+					<span class="grouped" v-else>
+						<li><a href="#" data-toggle="modal" data-target="#register">Register</a></li>
+						<li><a href="#" data-toggle="modal" data-target="#login">Login</a></li>
+					</span>
 				</ul>
 			</div>
 		</div>
@@ -30,10 +32,10 @@
 		border: 0;
 		border-radius: 0;
 		margin: 0;
-		min-height: 64px;
+		min-height: 50px;
 
 		.navbar-brand, li a, li a:hover, li a:focus {
-			line-height: 64px;
+			line-height: 50px;
 			padding: 0px 10px;
 			margin: 0px;
 			color: #fff;
@@ -44,6 +46,10 @@
 			color: #fff;
 		}
 
+		a {
+    		text-decoration: none;
+		}
+
 		.navbar-toggle, .navbar-toggle:hover, .navbar-toggle:focus {
 			border: 0;
 			background: 0;
@@ -56,5 +62,11 @@
 		.navbar-collapse {
 			border: 0;
 		}
+
+		.grouped {
+			margin: 0;
+    		display: flex;
+			text-decoration: none;
+		}
 	}
 </style>