The Wayback Machine - https://web.archive.org/web/20220406095905/https://github.com/vuejs/rfcs/pull/187
Skip to content
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

Merged
merged 3 commits into from Aug 5, 2020

Conversation

Copy link
Member

@posva posva commented Jul 8, 2020

@michalsnik
Copy link
Member

@michalsnik michalsnik commented Jul 13, 2020

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 return that can either contain a route to be redirected to, or false to cancel any further action.

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.

@posva
Copy link
Member Author

@posva posva commented Jul 13, 2020

Arrays in beforeEnter are already supported in v4 🙂

@michalsnik
Copy link
Member

@michalsnik michalsnik commented Jul 13, 2020

Nice! Can't wait to start using it and removing the extra logic we have in order to support it :)

@ByScripts
Copy link

@ByScripts ByScripts commented Jul 20, 2020

It's already possible to write router.beforeEach(() => next(isAuthenticated) IIRC

Edit: I'm not saying I'm not OK with this RFC thought :)

@posva
Copy link
Member Author

@posva posva commented Jul 20, 2020

it would become router.beforeEach(() => isAuthenticated)

@ByScripts
Copy link

@ByScripts ByScripts commented Jul 20, 2020

@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 👍

@posva
Copy link
Member Author

@posva posva commented Jul 28, 2020

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.
Final comments stage does not mean the RFC's design details are final - we may still tweak the details as we implement it and discover new technical insights or constraints. It may even be further adjusted based on user feedback after it lands in an alpha/beta release.
If no major objections with solid supporting arguments have been presented after a week, the RFC will be merged and become an active RFC.

@posva posva added the final comments label Jul 28, 2020
@posva posva force-pushed the router/navigation-guards-return branch from e1964a5 to 2b44734 Compare Aug 5, 2020
@posva posva merged commit 217adc6 into vuejs:master Aug 5, 2020
@posva posva deleted the router/navigation-guards-return branch Aug 5, 2020
@anthonygore
Copy link

@anthonygore anthonygore commented Aug 16, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
final comments router
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants
Morty Proxy This is a proxified and sanitized view of the page, visit original site.