keanu-weblite/src/router/index.js

49 lines
911 B
JavaScript
Raw Normal View History

2020-11-09 10:26:56 +01:00
import Vue from 'vue'
import VueRouter from 'vue-router'
import Chat from '../components/Chat.vue'
import Login from '../components/Login.vue'
Vue.use(VueRouter)
const routes = [
{
path: '/',
name: 'Chat',
component: Chat
},
{
path: '/login',
component: Login
},
2020-11-19 22:48:08 +01:00
{
path: '/join/:room?',
redirect: from => {
const room = from.hash;
if (room) {
return { name: 'Chat', params: { joinRoom: room }};
}
return '/';
}
},
2020-11-09 10:26:56 +01:00
]
const router = new VueRouter({
routes
})
router.beforeEach((to, from, next) => {
const publicPages = ['/login'];
const authRequired = !publicPages.includes(to.path);
const loggedIn = localStorage.getItem('user');
// trying to access a restricted page + not logged in
// redirect to login page
if (authRequired && !loggedIn) {
next('/login');
} else {
next();
}
});
export default router