New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow guards to return instead of calling the next callback
#187
Conversation
|
I really like the proposed change. In fact it's actually exactly what I was doing in my project with the help of extra utility I wrote to be able to more easily compose different guards. The individual guards look exactly as you propose here, with the It looks more or less like this: function authRequired(to, from) {
if (!isAuthenticated) {
return { name: routes.ROUTE_LOG_IN };
}
}
function emailVerificationRequired(to, from) {
if (!isVerified) {
return { name: routes.ROUTE_VERIFY_EMAIL };
}
}
async function orgAccessRequired(to, from) {
try {
await checkOrgAccess(to.params.orgId);
} catch (err) {
// ...
return { name: routes.ROUTE_403 }
}
}However, in order to keep things even easier to use and understand just by scanning routes file, we implemented ability to compose guards like so: {
path: "/dashboard",
name: routes.ROUTE_DASHBOARD,
component: () => import("@/pages/Dashboard.vue")
beforeEnter: composeGuards([
authRequired,
emailVerificationRequired,
orgAccessRequired
])
}So if we return in any guard, none of the following guards will be executed, this way we can create guards that are more easily testable and understanding guards logic is super easy. I think it might be a good idea to add support for this on the router level too. |
|
Arrays in |
|
Nice! Can't wait to start using it and removing the extra logic we have in order to support it :) |
|
It's already possible to write Edit: I'm not saying I'm not OK with this RFC thought :) |
|
it would become |
|
@posva Yes, I was answering @michalsnik saying he wants to remove extra logic :) Anyway, your proposal goes further by allowing the return of a Promise |
|
This RFC is now in final comments stage. An RFC in final comments stage means that: The core team has reviewed the feedback and reached consensus about the general direction of the RFC and believes that this RFC is a worthwhile addition to the framework. |
e1964a5
to
2b44734
Compare
|
Link should be updated to https://github.com/vuejs/rfcs/blob/master/active-rfcs/0037-router-return-guards.md |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

Rendered