stencil-router icon indicating copy to clipboard operation
stencil-router copied to clipboard

Route guards

Open jgw96 opened this issue 6 years ago • 10 comments

Ive gotten asked a few times about how to do route guards in the Stencil router. Just to be clear, by route guards i mean blocking transition to a route based on some condition. If that condition === true, navigate, if not , dont navigate. After talking to Josh it sounds like we have an API for this, but it does not have an external interface to that API. The purpose of this issue is to track adding an external interface for that API.

jgw96 avatar Jun 01 '18 19:06 jgw96

any updates regarding this issue? I am facing a lot of bad experience do to that issue, I have to do some checks before loading the router, now I had to do it inside the componentWillLoad method which breaks the navigation.

MoAsmar avatar Aug 17 '18 11:08 MoAsmar

@MoAsmar Can you provide more detail on the experience? What is it exactly you would like to accomplish. It is possible there might be another mechanism that would work.

jthoms1 avatar Aug 17 '18 14:08 jthoms1

I want to call an API before routing, get the response and check for a condition to specify whether I want to redirect to a route or to another route or stay at the same route, same as CanActivate Guard from Angular.

Note that doing this while loading the component means that I am already in a route in the browser history.

MoAsmar avatar Aug 27 '18 08:08 MoAsmar

Makes sense. Thanks! I will hopefully be completing this in the next couple weeks.

jthoms1 avatar Aug 27 '18 17:08 jthoms1

Any updates regarding this issue ?

MoAsmar avatar Sep 20 '18 08:09 MoAsmar

I created a Prompt component ala react router. That should accomplish the before leave route guard. An example of this can be seen here. https://github.com/ionic-team/stencil-router/blob/master/packages/demo/src/components/test-route-guard.tsx#L22

I haven't had time to write proper docs yet, but I hope that you will be able to give it a try. Thanks!

jthoms1 avatar Sep 25 '18 03:09 jthoms1

I didn't get it, its just an alert window inside a component, can you give me an explanation of it please. Because I what I wanted to have is a guard that passed as property on the route definition itself like the below: image so when the user navigates to route called for example '/test-route' a method (isAuthenticated) is called and check the user identity authentication, if he is not authenticated > I redirect him from inside the method to a signup page, else > I let him access the route.

The purpose of this is to prevent adding unwanted routes to the browser history, so the '/test-route' is not accessed at all so its not in the history, so when I click back it doesn't take me to it. I hope that you understand my point.

MoAsmar avatar Sep 28 '18 10:09 MoAsmar

@MoAsmar for your last case, you need to check privateRoute example. As I understand @jthoms1 means different thing by route guard.

val-o avatar Dec 23 '18 06:12 val-o

Can we please get a follow up on this issue? I'm currently checking for authentication in componentWillLoad, but as @MoAsmar mentioned we are already inside the route we want to prevent navigation to.

daftclaud avatar Sep 28 '19 15:09 daftclaud

@jthoms1 We need something like this, Angular CanActivate

MoAsmar avatar Sep 30 '19 08:09 MoAsmar