keanu-weblite/src/router/index.js
N-Pex 6608f0c8ce Expose room id in navigation
So rooms can be bookmarked, issue #26.
2021-01-12 11:26:01 +01:00

68 lines
1.4 KiB
JavaScript

import Vue from 'vue'
import VueRouter from 'vue-router'
//import Home from '../components/Home.vue'
import Chat from '../components/Chat.vue'
import Login from '../components/Login.vue'
import util from '../plugins/utils'
Vue.use(VueRouter)
const routes = [
{
path: '/',
name: 'Home',
component: Chat
},
{
path: '/room/:roomId',
name: 'Chat',
component: Chat
},
{
path: '/info',
name: 'RoomInfo',
component: () => import('../components/RoomInfo.vue'),
props: true,
},
{
path: '/login',
name: 'Login',
component: Login,
props: true
},
{
path: '/join/',
name: 'Join',
component: () => import('../components/Join.vue'),
props: true
},
]
const router = new VueRouter({
routes
});
router.beforeEach((to, from, next) => {
const publicPages = ['/login'];
var authRequired = !publicPages.includes(to.path) && !to.path.startsWith('/join');
const loggedIn = localStorage.getItem('user');
if (to.name == 'Chat') {
const roomId = util.sanitizeRoomId(to.params.roomId);
router.app.$matrix.setCurrentRoomId(roomId);
if (roomId && roomId.startsWith('#')) {
//Invite to public room
authRequired = false;
}
}
// trying to access a restricted page + not logged in
// redirect to login page
if (authRequired && !loggedIn) {
next('/login');
} else {
next();
}
});
export default router