Cleanup vue router route guards

This commit is contained in:
Pavel Djundik 2020-07-20 11:38:12 +03:00
parent 0ac1fcb471
commit 181a198994
2 changed files with 79 additions and 59 deletions

View File

@ -32,12 +32,58 @@ const router = new VueRouter({
next(); next();
}, },
}, },
{
name: "Connect",
path: "/connect",
component: Connect,
props: (route) => ({queryParams: route.query}),
},
{
name: "Settings",
path: "/settings",
component: Settings,
},
{
name: "Help",
path: "/help",
component: Help,
},
{
name: "Changelog",
path: "/changelog",
component: Changelog,
},
{
name: "NetworkEdit",
path: "/edit-network/:uuid",
component: NetworkEdit,
},
{
name: "RoutedChat",
path: "/chan-:id",
component: RoutedChat,
},
], ],
}); });
router.beforeEach((to, from, next) => {
// If user is not yet signed in, wait for appLoaded state to change
// unless they are trying to open SignIn (which can be triggered in auth.js)
if (!store.state.appLoaded && to.name !== "SignIn") {
store.watch(
(state) => state.appLoaded,
() => next()
);
return;
}
next();
});
router.beforeEach((to, from, next) => { router.beforeEach((to, from, next) => {
// Disallow navigating to non-existing routes // Disallow navigating to non-existing routes
if (store.state.appLoaded && !to.matched.length) { if (!to.matched.length) {
next(false); next(false);
return; return;
} }
@ -91,42 +137,6 @@ router.afterEach((to) => {
} }
}); });
function initialize() {
router.addRoutes([
{
name: "Connect",
path: "/connect",
component: Connect,
props: (route) => ({queryParams: route.query}),
},
{
name: "Settings",
path: "/settings",
component: Settings,
},
{
name: "Help",
path: "/help",
component: Help,
},
{
name: "Changelog",
path: "/changelog",
component: Changelog,
},
{
name: "NetworkEdit",
path: "/edit-network/:uuid",
component: NetworkEdit,
},
{
name: "RoutedChat",
path: "/chan-:id",
component: RoutedChat,
},
]);
}
function navigate(routeName, params = {}) { function navigate(routeName, params = {}) {
if (router.currentRoute.name) { if (router.currentRoute.name) {
router.push({name: routeName, params}).catch(() => {}); router.push({name: routeName, params}).catch(() => {});
@ -156,4 +166,4 @@ if ("serviceWorker" in navigator) {
}); });
} }
export {initialize, router, navigate, switchToChannel}; export {router, navigate, switchToChannel};

View File

@ -1,8 +1,9 @@
"use strict"; "use strict";
import Vue from "vue";
import socket from "../socket"; import socket from "../socket";
import storage from "../localStorage"; import storage from "../localStorage";
import {router, switchToChannel, navigate, initialize as routerInitialize} from "../router"; import {router, switchToChannel, navigate} from "../router";
import store from "../store"; import store from "../store";
import parseIrcUri from "../helpers/parseIrcUri"; import parseIrcUri from "../helpers/parseIrcUri";
@ -16,10 +17,6 @@ socket.on("init", function (data) {
} }
if (!store.state.appLoaded) { if (!store.state.appLoaded) {
// Routes are initialized after networks data is merged
// so the route guard for channels works correctly on page load
routerInitialize();
store.commit("appLoaded"); store.commit("appLoaded");
socket.emit("setting:get"); socket.emit("setting:get");
@ -28,7 +25,12 @@ socket.on("init", function (data) {
window.g_TheLoungeRemoveLoading(); window.g_TheLoungeRemoveLoading();
} }
// TODO: Review this code and make it better Vue.nextTick(() => {
// If we handled query parameters like irc:// links or just general
// connect parameters in public mode, then nothing to do here
if (!handleQueryParams()) {
// If we are on an unknown route or still on SignIn component
// then we can open last known channel on server, or Connect window if none
if (!router.currentRoute.name || router.currentRoute.name === "SignIn") { if (!router.currentRoute.name || router.currentRoute.name === "SignIn") {
const channel = store.getters.findChannel(data.active); const channel = store.getters.findChannel(data.active);
@ -42,10 +44,8 @@ socket.on("init", function (data) {
navigate("Connect"); navigate("Connect");
} }
} }
if ("URLSearchParams" in window) {
handleQueryParams();
} }
});
} }
}); });
@ -154,6 +154,10 @@ function mergeChannelData(oldChannels, newChannels) {
} }
function handleQueryParams() { function handleQueryParams() {
if (!("URLSearchParams" in window)) {
return false;
}
const params = new URLSearchParams(document.location.search); const params = new URLSearchParams(document.location.search);
const cleanParams = () => { const cleanParams = () => {
@ -169,11 +173,17 @@ function handleQueryParams() {
cleanParams(); cleanParams();
router.push({name: "Connect", query: queryParams}); router.push({name: "Connect", query: queryParams});
return true;
} else if (document.body.classList.contains("public") && document.location.search) { } else if (document.body.classList.contains("public") && document.location.search) {
// Set default connection settings from url params // Set default connection settings from url params
const queryParams = Object.fromEntries(params.entries()); const queryParams = Object.fromEntries(params.entries());
cleanParams(); cleanParams();
router.push({name: "Connect", query: queryParams}); router.push({name: "Connect", query: queryParams});
return true;
} }
return false;
} }