Skip to content

Instantly share code, notes, and snippets.

@ivalkenburg
Created July 5, 2018 14:11
Show Gist options
  • Save ivalkenburg/3e73b98a0c9b1c8dae71bd1c76363360 to your computer and use it in GitHub Desktop.
Save ivalkenburg/3e73b98a0c9b1c8dae71bd1c76363360 to your computer and use it in GitHub Desktop.
Vue-router guards
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 };
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