import { createRouter , createWebHistory } from 'vue-router' ; import { useAuth } from '../modules/auth' ; import Home from '../views/Home.vue' ; const routes = [ { path : '/' , name : 'home' , component : Home , meta : { requiresAuth : true } , } , { path : '/register' , name : 'register' , component : ( ) => import ( '../views/Register.vue' ) , meta : { requiresAuth : false } , } , { path : '/login' , name : 'login' , component : ( ) => import ( '../views/Login.vue' ) , meta : { requiresAuth : false } , } , { path : '/logout' , name : 'logout' , component : ( ) => import ( '../views/Logout.vue' ) , meta : { requiresAuth : true } , } , { path : '/about' , name : 'about' , component : ( ) => import ( '../views/About.vue' ) , meta : { requiresAuth : true } , } , { path : '/genres' , name : 'genres.list' , component : Home , meta : { requiresAuth : true } , } , { path : '/genres/:id' , name : 'genres.view' , component : ( ) => import ( '../views/genres/View.vue' ) , meta : { requiresAuth : true } , } , { path : '/movies/' , name : 'movies.list' , component : Home , meta : { requiresAuth : true } , } , { path : '/movies/:id' , name : 'movies.view' , component : ( ) => import ( '../views/movies/View.vue' ) , meta : { requiresAuth : true } , } , { path : '/subscribe' , name : 'subscribe' , component : ( ) => import ( '../views/Subscribe.vue' ) , meta : { requiresAuth : true } , } , { path : '/subscribe/success' , name : 'subscribe.success' , component : ( ) => import ( '../views/SubscribeSuccess.vue' ) , } , { path : '/subscribe/cancelled' , name : 'subscribe.cancelled' , component : ( ) => import ( '../views/SubscribeCancelled.vue' ) , } , { path : '/account' , name : 'account' , component : ( ) => import ( '../views/Account.vue' ) , } , { path : '/*' , redirect : '/' , } , ] ; const router = createRouter ( { history : createWebHistory ( process . env . BASE_URL ) , routes } ) ; router . beforeEach ( ( to , from , next ) => { const { authenticating , user } = useAuth ( ) ; if ( authenticating . value === false && to . meta . requiresAuth === true && ! user ?. value ) { console . log ( 'requires auth, redirect to login' ) ; next ( { name : 'login' } ) ; } else next ( ) ; } ) ; export default router ;