Hello everyone!
I’m having a rough time trying to make a router with authentication work and I’m really out of ideas here.
The problem is that for some reason when navigating from one route to another, the page that should render stays with the “ion-page-invisible” class and thus I get a blank page when navigating to that route.
So that you can understand a little better, I’ll show you the code I have.
I have a PrivateRoute component that looks like this:
Also a PublicRoute component that looks like this:
So you get the idea, if the user is logged, then go to the component of the route, if not, then redirect to where I’m saying.
This is my main Router:
And this is the MainRoutes component that’s being rendered when calling the PrivateRoute with the “/” route shown there:
When I navigate from welcome to Login it works ok, when I set the state of the user to logged from the Login, the PublicRoute logic kicks in and I get to the /credential route just fine, it renders the component ok.
The problem appears when I set the state of the user to logged out from /credential, this should make the logic from PrivateRoute to kick in and redirect me to /Welcome again, which it does, but I get this:
As you can see, a blank page with the divs containing the “ion-page-invisible” classes, if I provoke any changes to the component or simply refresh the page, it renders ok.
I couldn’t find a solution for this problem yet, I’ve tried every piece of code out there, but nothing seems to work.
Is it a problem with IonRouterOutlet? Cause as you can see, I have 2 defined. One with the PublicRoutes and one inside the MainRoutes component which I use to render the Menu once you are logged.
Please if you have any idea I would be really grateful.
Have a nice day Folks!
EDIT: Things that I’ve noticed:
When I’m inside the /credential route, rendering the component, I see the two ion-router-outlets on the DOM, the one that contains the public routes, which is the one without ID, is empty:
While here, if I refresh the page before clicking the exit button (which triggers the state change to logout) and then I click it, everything works fine, the PrivateRoute logic kicks in, I get redirected to /welcome and the component renders ok.
So, why when I change to another ion-router-outlet the old one is still in the DOM? It has anything to do with the problem I’m having?
EDIT 2: If I remove the ion-router-outlet that wraps the PublicRoute components, everything works fine, so it is indeed a problem with the ion-router-outlet:
Anyone has had this problem before?
I’m really sorry for all this questions, I’m kinda new to Ionic and I couldn’t find a solution for this on any post or github ticket.
Thank you again!