keanu-weblite/src/router/index.js

69 lines
1.4 KiB
JavaScript
Raw Normal View History

2020-11-09 10:26:56 +01:00
import Vue from 'vue'
import VueRouter from 'vue-router'
2021-01-11 17:42:58 +01:00
//import Home from '../components/Home.vue'
2020-11-09 10:26:56 +01:00
import Chat from '../components/Chat.vue'
import Login from '../components/Login.vue'
import util from '../plugins/utils'
2020-11-09 10:26:56 +01:00
Vue.use(VueRouter)
const routes = [
{
path: '/',
2021-01-11 17:42:58 +01:00
name: 'Home',
component: Chat
},
{
path: '/room/:roomId',
2020-11-09 10:26:56 +01:00
name: 'Chat',
component: Chat
},
2020-12-04 17:15:18 +01:00
{
path: '/info',
name: 'RoomInfo',
component: () => import('../components/RoomInfo.vue'),
props: true,
},
2020-11-09 10:26:56 +01:00
{
path: '/login',
2021-01-08 11:07:02 +01:00
name: 'Login',
component: Login,
props: true
2020-11-09 10:26:56 +01:00
},
2020-11-19 22:48:08 +01:00
{
2020-11-25 10:02:24 +01:00
path: '/join/',
2021-01-11 17:42:58 +01:00
name: 'Join',
2020-11-25 10:02:24 +01:00
component: () => import('../components/Join.vue'),
props: true
2020-11-19 22:48:08 +01:00
},
2020-11-09 10:26:56 +01:00
]
const router = new VueRouter({
routes
2021-01-11 17:42:58 +01:00
});
2020-11-09 10:26:56 +01:00
router.beforeEach((to, from, next) => {
const publicPages = ['/login'];
2021-01-11 17:42:58 +01:00
var authRequired = !publicPages.includes(to.path) && !to.path.startsWith('/join');
2020-11-09 10:26:56 +01:00
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('#')) {
2021-01-11 17:42:58 +01:00
//Invite to public room
authRequired = false;
}
}
2020-11-09 10:26:56 +01:00
// trying to access a restricted page + not logged in
// redirect to login page
if (authRequired && !loggedIn) {
next('/login');
} else {
next();
}
});
export default router