Skip to content

Instantly share code, notes, and snippets.

@apavel3458

apavel3458/router.js Secret

Created Feb 28, 2020
Embed
What would you like to do?
// import { notAuthenticated } from './axios'
import { Notify } from 'quasar'
// export default ({ router, store, Vue }) => {
// router.beforeEach((to, from, next) => {
// // Now you need to add your authentication logic here, like calling an API endpoint
// alert('beforeach')
// })
// }
let routerInstance = void 0
// export default async ({ router }) => {
// // something to do
// routerInstance = router
// }
export default ({ router, store, Vue }) => {
router.beforeEach((to, from, next) => {
const adminRequired = to.matched.some(record => record.meta.admin)
const authenticatedRequired = to.matched.some(record => record.meta.private)
if (authenticatedRequired && !store.getters['security/isAuthenticated']) {
// this route requires auth, checked if logged in
Notify.create({ color: 'negative', message: 'You must log in to access this page', icon: 'report_problem' })
store.dispatch('security/setRedirect', to.fullPath)
return next({
name: 'login'
// query: { redirect: to.fullPath }
})
}
if (adminRequired && !store.getters['security/isAdmin']) {
return next('/dashboard')
}
next()
})
routerInstance = router
}
export { routerInstance }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment