1
0
Fork 0
mirror of https://github.com/VueTorrent/VueTorrent.git synced 2025-04-02 06:45:06 +03:00
VueTorrent/src/router.js
2020-05-24 11:50:21 +02:00

52 lines
1.3 KiB
JavaScript

import Vue from 'vue'
import Router from 'vue-router'
import Dashboard from '@/views/Dashboard.vue'
import Login from '@/views/Login.vue'
import { isAuthenticated } from '@/services/auth.js'
Vue.use(Router)
const router = new Router({
mode: 'history',
base: process.env.BASE_URL,
routes: [
{
path: '/',
name: 'dashboard',
component: Dashboard
},
{
path: '/login',
name: 'login',
component: Login,
meta: {
public: true, // Allow access to even if not logged in
onlyWhenLoggedOut: true
}
}
]
})
router.beforeEach(async (to, from, next) => {
const isPublic = to.matched.some(record => record.meta.public)
const onlyWhenLoggedOut = to.matched.some(
record => record.meta.onlyWhenLoggedOut
)
const authenticated = await isAuthenticated()
if (!isPublic && !authenticated) {
return next({
path: '/login',
query: { redirect: to.fullPath } // Store the full path to redirect the user to after login
})
}
// Do not allow user to visit login page or register page if they are logged in
if (authenticated && onlyWhenLoggedOut) {
return next('/')
}
next()
})
export default router