Created
July 5, 2018 14:11
-
-
Save ivalkenburg/3e73b98a0c9b1c8dae71bd1c76363360 to your computer and use it in GitHub Desktop.
Vue-router guards
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
function guestGuard(routes) { | |
return routes.map(route => { | |
return { | |
...route, | |
beforeEnter: (to, from, next) => { | |
if(confirm('continue guest guard?')) { | |
next(); | |
} else { | |
next(false); | |
} | |
} | |
}; | |
}); | |
} | |
function authGuard(routes) { | |
return routes.map(route => { | |
return { | |
...route, | |
beforeEnter: (to, from, next) => { | |
if (confirm('continue auth guard?')) { | |
next(); | |
} else { | |
next(false); | |
} | |
} | |
}; | |
}); | |
} | |
export { guestGuard, authGuard }; |
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 Home from './../views/Home'; | |
import Hello from './../views/Hello'; | |
import Login from './../views/Login'; | |
import Register from './../views/Register'; | |
import {authGuard, guestGuard} from './guards'; | |
export default [ | |
{ | |
path: '/', | |
name: 'home', | |
component: Home | |
}, | |
...authGuard([ | |
{ | |
path: '/hello', | |
name: 'hello', | |
component: Hello | |
} | |
]), | |
...guestGuard([ | |
{ | |
path: '/login', | |
name: 'login', | |
component: Login | |
}, | |
{ | |
path: '/register', | |
name: 'register', | |
component: Register | |
} | |
]) | |
]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment