Last active
March 22, 2019 20:46
-
-
Save igorkosta/98f4a878c912662d03bab1f82d5557bf to your computer and use it in GitHub Desktop.
Vue Router
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Vue from 'vue' | |
import Router from 'vue-router' | |
import store from '@/store/index.js' | |
import routes from '@/router/routes/index.js' | |
Vue.use(Router) | |
const router = new Router({ | |
routes: [ | |
{ | |
path: '/', | |
redirect: '/dashboard' | |
} | |
].concat(routes) | |
}) | |
router.beforeEach((to, from, next) => { | |
const authenticated = store.state.user.authenticated | |
const onlyLoggedOut = to.matched.some(record => record.meta.onlyLoggedOut) | |
const isPublic = to.matched.some(record => record.meta.public) | |
if (!isPublic && !authenticated) { | |
// this route requires auth, check if logged in | |
// if not, redirect to login page. | |
return next({ | |
path: '/login', | |
query: { redirect: to.fullPath } | |
}) | |
} | |
if (authenticated && onlyLoggedOut) { | |
return next('/') | |
} | |
next() | |
}) | |
export default router |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Accounts from '@/views/Accounts/Index.vue' | |
import Dashboard from '@/views/Dashboard/Index.vue' | |
import Settings from '@/views/Settings/Index.vue' | |
const routes = [ | |
{ | |
path: '/accounts', | |
name: 'accounts', | |
component: Accounts | |
}, | |
{ | |
path: '/dashboard', | |
name: 'dashboard', | |
component: Dashboard | |
}, | |
{ | |
path: '/settings', | |
name: 'settings', | |
component: Settings | |
} | |
] | |
export default routes.map(route => { | |
return { ...route, meta: { public: false } } | |
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Login from '@/views/Login.vue' | |
import Registration from '@/views/Registration.vue' | |
import ForgotPassword from '@/views/ForgotPassword.vue' | |
const routes = [ | |
{ | |
path: '/login', | |
name: 'login', | |
component: Login | |
}, | |
{ | |
path: '/registration', | |
name: 'registration', | |
component: Registration | |
}, | |
{ | |
path: '/forgot-password', | |
name: 'forgotPassword', | |
component: ForgotPassword | |
} | |
] | |
export default routes.map(route => { | |
const meta = { | |
public: true, | |
onlyLoggedOut: true | |
} | |
return { ...route, meta } | |
}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment