keanu-weblite/src/router/index.js

134 lines
3 KiB
JavaScript
Raw Normal View History

2020-11-09 10:26:56 +01:00
import Vue from 'vue'
import VueRouter from 'vue-router'
2021-02-09 12:37:56 +01:00
import Home from '../components/Home.vue'
2020-11-09 10:26:56 +01:00
import Chat from '../components/Chat.vue'
2021-01-12 20:28:23 +01:00
import Join from '../components/Join.vue'
2020-11-09 10:26:56 +01:00
import Login from '../components/Login.vue'
import Profile from '../components/Profile.vue'
2021-03-23 16:20:01 +01:00
import CreateRoom from '../components/CreateRoom.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',
2021-02-09 12:37:56 +01:00
component: Home
2021-01-11 17:42:58 +01:00
},
{
path: '/room/:roomId?',
2020-11-09 10:26:56 +01:00
name: 'Chat',
component: Chat,
meta: {
includeRoom: true
}
2020-11-09 10:26:56 +01:00
},
2020-12-04 17:15:18 +01:00
{
path: '/info',
name: 'RoomInfo',
component: () => import('../components/RoomInfo.vue'),
props: true,
meta: {
title: 'Info',
includeRoom: true
}
2020-12-04 17:15:18 +01:00
},
{
path: '/profile',
name: 'Profile',
component: Profile,
meta: {
title: 'Profile'
}
},
2021-03-23 16:20:01 +01:00
{
path: '/createroom',
name: 'CreateRoom',
component: CreateRoom,
meta: {
title: 'Create room'
2021-03-23 16:20:01 +01:00
}
},
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
{
2021-01-12 20:28:23 +01:00
path: '/join/:roomId?',
2021-01-11 17:42:58 +01:00
name: 'Join',
2021-01-12 20:28:23 +01:00
component: Join
2020-11-19 22:48:08 +01:00
},
{
path: '/invite/:roomId?',
name: 'Invite',
component: () => import('../components/Invite.vue'),
meta: {
title: 'Add Friends'
}
2021-04-02 10:58:58 +02:00
},
{
path: '/goodbye',
name: 'Goodbye',
component: () => import('../components/QuoteView.vue'),
props: true
}
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', '/createroom'];
2021-01-12 20:28:23 +01:00
var authRequired = !publicPages.includes(to.path);
const loggedIn = router.app.$store.state.auth.user;
2020-11-09 10:26:56 +01:00
if (to.query && to.query.lang) {
// Set language via query param
const lang = to.query.lang;
// Check if valid translation
if (router.app.$i18n.messages[lang]) {
router.app.$store.commit('setLanguage', lang);
if (router.app.$i18n) {
router.app.$i18n.locale = to.query.lang;
}
}
}
2021-01-12 20:28:23 +01:00
if (to.name == 'Chat' || to.name == 'Join') {
if (!to.params.roomId && to.hash) {
// Public rooms start with '#', confuses the router. If hash but no roomId param, set it.
to.params.roomId = to.hash;
}
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;
}
} else if (to.name == 'Invite') {
if (to.params.roomId) {
const roomId = util.sanitizeRoomId(to.params.roomId);
router.app.$matrix.setCurrentRoomId(roomId);
}
2021-01-11 17:42:58 +01:00
}
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();
}
});
router.getRoomLink = function (roomId) {
2021-02-24 15:11:39 +01:00
return window.location.origin + window.location.pathname + "#/room/" + encodeURIComponent(util.sanitizeRoomId(roomId));
2021-01-21 12:12:55 +01:00
}
2020-11-09 10:26:56 +01:00
export default router