Quellcode durchsuchen

fix: route loginRequired/guestsOnly/adminRequired didn't work on first load, reset_password didn't have guestsOnly enabled

Kristian Vos vor 3 Jahren
Ursprung
Commit
937d2da798
1 geänderte Dateien mit 42 neuen und 43 gelöschten Zeilen
  1. 42 43
      frontend/src/main.js

+ 42 - 43
frontend/src/main.js

@@ -118,7 +118,10 @@ const router = createRouter({
 		},
 		{
 			path: "/reset_password",
-			component: () => import("@/pages/ResetPassword.vue")
+			component: () => import("@/pages/ResetPassword.vue"),
+			meta: {
+				guestsOnly: true
+			}
 		},
 		{
 			path: "/set_password",
@@ -164,6 +167,44 @@ const router = createRouter({
 	]
 });
 
+router.beforeEach((to, from, next) => {
+	if (window.stationInterval) {
+		clearInterval(window.stationInterval);
+		window.stationInterval = 0;
+	}
+
+	if (ws.socket && to.fullPath !== from.fullPath) {
+		ws.clearCallbacks();
+		ws.destroyListeners();
+	}
+
+	if (to.meta.loginRequired || to.meta.adminRequired || to.meta.guestsOnly) {
+		const gotData = () => {
+			if (to.meta.loginRequired && !store.state.user.auth.loggedIn)
+				next({ path: "/login" });
+			else if (
+				to.meta.adminRequired &&
+				store.state.user.auth.role !== "admin"
+			)
+				next({ path: "/" });
+			else if (to.meta.guestsOnly && store.state.user.auth.loggedIn)
+				next({ path: "/" });
+			else next();
+		};
+
+		if (store.state.user.auth.gotData) gotData();
+		else {
+			const watcher = store.watch(
+				state => state.user.auth.gotData,
+				() => {
+					watcher();
+					gotData();
+				}
+			);
+		}
+	} else next();
+});
+
 app.use(router);
 
 lofig.folder = "../config/default.json";
@@ -242,47 +283,5 @@ lofig.folder = "../config/default.json";
 			);
 	});
 
-	router.beforeEach((to, from, next) => {
-		if (window.stationInterval) {
-			clearInterval(window.stationInterval);
-			window.stationInterval = 0;
-		}
-
-		if (ws.socket && to.fullPath !== from.fullPath) {
-			ws.clearCallbacks();
-			ws.destroyListeners();
-		}
-
-		if (
-			to.meta.loginRequired ||
-			to.meta.adminRequired ||
-			to.meta.guestsOnly
-		) {
-			const gotData = () => {
-				if (to.meta.loginRequired && !store.state.user.auth.loggedIn)
-					next({ path: "/login" });
-				else if (
-					to.meta.adminRequired &&
-					store.state.user.auth.role !== "admin"
-				)
-					next({ path: "/" });
-				else if (to.meta.guestsOnly && store.state.user.auth.loggedIn)
-					next({ path: "/" });
-				else next();
-			};
-
-			if (store.state.user.auth.gotData) gotData();
-			else {
-				const watcher = store.watch(
-					state => state.user.auth.gotData,
-					() => {
-						watcher();
-						gotData();
-					}
-				);
-			}
-		} else next();
-	});
-
 	app.mount("#root");
 })();