import Vue from 'vue' ; import VueRouter , { RouteConfig } from 'vue-router' ; import Auth from '../auth' ; Vue . use ( VueRouter ) ; summary enum export enum Guard { RequiresAuth , NoAuth , Universal } export const routes = [ { path : '/' , name : 'Home' , component : ( ) => import ( '../views/Home.vue' ) , } , { path : '/register' , name : 'Register' , component : ( ) => import ( '@/views/Register.vue' ) , guardType : Guard . NoAuth } , { path : '/login' , name : 'Login' , component : ( ) => import ( '@/views/Login.vue' ) , guardType : Guard . NoAuth } , { path : '/settings' , name : 'Settings' , component : ( ) => import ( '@/views/AccountSettings.vue' ) , guardType : Guard . RequiresAuth } , { path : '/logout' , name : 'Logout' , component : ( ) => import ( '@/views/Logout.vue' ) , guardType : Guard . RequiresAuth } , { path : '/resetpassword' , name : 'ResetPassword' , component : ( ) => import ( '@/views/ResetPassword.vue' ) , guardType : Guard . RequiresAuth } , { path : '/recover' , name : 'RecoverAccount' , component : ( ) => import ( '@/views/RecoverAccount.vue' ) , guardType : Guard . NoAuth } , { path : '/verify' , name : 'Verify Email' , component : ( ) => import ( '@/views/VerifyEmail.vue' ) , guardType : Guard . RequiresAuth } ] ; const getRouteByName = ( name ) : RouteConfig | any => { return routes . find ( ( route ) : boolean => route . name === name ) ; } ; summary const beforeEnter = async ( to , from , next ) => { todo const isLoggedIn = await Auth . authenticate ( ) ; if ( ! to . name ) return next ( ) ; const route = getRouteByName ( to && to . name ) ; if ( ( isLoggedIn && route . guardType === Guard . NoAuth ) || ( ! isLoggedIn && route . guardType === Guard . RequiresAuth ) ) return router . push ( { name : 'Home' } ) ; next ( ) ; } ; summary returns const mapRouteGuards = ( ) => { const rval = routes . map ( ( route ) : RouteConfig | any => { if ( route . guardType !== undefined ) { route . beforeEnter = beforeEnter ; } return route ; } ) ; return rval ; } ; const router = new VueRouter ( { mode : 'history' , base : process . env . BASE_URL , linkExactActiveClass : 'active' , routes : mapRouteGuards ( ) } ) ; export default router ;