import Vue from 'vue' ; import VueRouter , { RouteConfig , NavigationGuard , Route , Location } from 'vue-router' ; import Login from '../views/Login.vue' ; import Signup from '../views/Signup.vue' ; import jwt from '@/helpers/jwt.helper' ; type NextFn = ( to ? : string | false | void | Location | ( ( ( vm : Vue ) => any ) ) | undefined ) => void ; const beforeEnter = ( to , from , next ) => { const isAuthenticated = jwt . isAuthenticated ( ) ; const needsAuth = to . meta && to . meta . auth === true ; if ( needsAuth ) { if ( ! isAuthenticated ) return next ( '/login' ) ; } else if ( isAuthenticated && ( to . name === 'login' || to . name === 'signup' ) ) { return next ( '/users' ) ; } return next ( ) ; } ; Vue . use ( VueRouter ) ; const routes = [ { path : '/' , name : 'login' , component : Login , beforeEnter , meta : { nav : false , auth : false } } , { path : '/signup' , name : 'signup' , component : Signup , beforeEnter , meta : { nav : false , auth : false } } , { path : '/users' , name : 'users' , component : ( ) => import ( '../views/Users.vue' ) , beforeEnter , meta : { nav : true , auth : true } } ] ; const router = new VueRouter ( { routes } ) ; export default router ;