Prechádzať zdrojové kódy

Added frontend admin and login hooks.

KrisVos130 8 rokov pred
rodič
commit
b85546ccef

+ 8 - 1
backend/logic/io.js

@@ -6,6 +6,7 @@ const app = require('./app');
 const actions = require('./actions');
 const cache = require('./cache');
 const utils = require('./utils');
+const db = require('./db');
 
 module.exports = {
 
@@ -98,7 +99,13 @@ module.exports = {
 						if (err2 && err2 !== true) {
 							socket.emit('ready', false);
 						} else if (userSession) {
-							socket.emit('ready', true);
+							db.models.user.findOne({ _id: userSession.userId }, (err, user) => {
+								let role = 'default';
+								if (user) {
+									role = user.role;
+								}
+								socket.emit('ready', true, role);
+							});
 						} else {
 							socket.emit('ready', false);
 						}

+ 6 - 4
frontend/App.vue

@@ -14,6 +14,7 @@
 	import WhatIsNew from './components/Modals/WhatIsNew.vue';
 	import LoginModal from './components/Modals/Login.vue';
 	import RegisterModal from './components/Modals/Register.vue';
+	import auth from './auth';
 
 	export default {
 		replace: false,
@@ -29,6 +30,7 @@
 					password: ""
 				},
 				loggedIn: false,
+				role: '',
 				isRegisterActive: false,
 				isLoginActive: false
 			}
@@ -47,10 +49,10 @@
 		},
 		ready() {
 			let _this = this;
-			lofig.folder = '../config/default.json';
-			lofig.get('socket.url', function(res) {
-				let socket = _this.socket = io(window.location.protocol + '//' + res);
-				socket.on("ready", status => _this.loggedIn = status);
+			auth.getStatus((authenticated, role) => {
+				_this.socket = window.socket;
+				_this.loggedIn = authenticated;
+				_this.role = role;
 			});
 		},
 		events: {

+ 26 - 0
frontend/auth.js

@@ -0,0 +1,26 @@
+let callbacks = [];
+
+export default {
+
+	ready: false,
+	authenticated: false,
+	role: 'default',
+
+	getStatus: function(cb) {
+		if (this.ready) {
+			cb(this.authenticated, this.role);
+		} else {
+			callbacks.push(cb);
+		}
+	},
+
+	data: function(authenticated, role) {
+		this.authenticated = authenticated;
+		this.role = role;
+		this.ready = true;
+		callbacks.forEach((callback) => {
+			callback(authenticated, role);
+		});
+		callbacks = [];
+	}
+}

+ 5 - 11
frontend/components/MainHeader.vue

@@ -13,7 +13,7 @@
 		</span>-->
 
 		<div class="nav-right">
-			<a class="nav-item is-tab admin" href="#" v-link="{ path: '/admin' }" v-if="isAdmin">
+			<a class="nav-item is-tab admin" href="#" v-link="{ path: '/admin' }" v-if="$parent.$parent.role === 'admin'">
 				Admin
 			</a>
 			<a class="nav-item is-tab" href="#">
@@ -46,24 +46,18 @@
 	export default {
 		data() {
 			return {
-				isAdmin: false
+
 			}
 		},
 		ready: function() {
 			let _this = this;
-			let socketInterval = setInterval(() => {
-				if (!!_this.$parent.socket) {
-					_this.socket = _this.$parent.socket;
-					_this.socket.emit('users.findBySession', res => {
-						if (res.status == 'success') _this.isAdmin = (res.data.role === 'admin');
-					});
-					clearInterval(socketInterval);
-				}
-			}, 100);
 		},
 		methods: {
 			toggleModal: function (type) {
 				this.$dispatch('toggleModal', type);
+			},
+			isAdmin: function() {
+				_this = this;
 			}
 		}
 	}

+ 31 - 3
frontend/main.js

@@ -1,7 +1,7 @@
 import Vue from 'vue';
 import VueRouter from 'vue-router';
-
 import App from './App.vue';
+import auth from './auth';
 
 import NotFound from './components/404.vue';
 import Home from './components/pages/Home.vue';
@@ -14,6 +14,32 @@ import Settings from './components/User/Settings.vue';
 Vue.use(VueRouter);
 
 let router = new VueRouter({ history: true });
+let _this = this;
+
+lofig.folder = '../config/default.json';
+
+lofig.get('socket.url', function(res) {
+	let socket = window.socket = io(window.location.protocol + '//' + res);
+	socket.on("ready", (status, role) => {
+		auth.data(status, role);
+	});
+});
+
+router.beforeEach((transition) => {
+	if (transition.to.loginRequired || transition.to.adminRequired) {
+		auth.getStatus((authenticated, role) => {
+			if (transition.to.loginRequired && !authenticated) {
+				transition.redirect('/login')
+			} else if (transition.to.adminRequired && role !== 'admin') {
+				transition.redirect('/adminRequired');
+			} else {
+				transition.next();
+			}
+		});
+	} else {
+		transition.next()
+	}
+});
 
 router.map({
 	'/': {
@@ -29,10 +55,12 @@ router.map({
 		component: User
 	},
 	'/settings': {
-		component: Settings
+		component: Settings,
+		loginRequired: true
 	},
 	'/admin': {
-		component: Admin
+		component: Admin,
+		adminRequired: true
 	},
 	'/official/:id': {
 		component: Station