This is probably a frequented question or topic of discussion. I feel like I have researched quite a bit but do not feel like I have received a solid answer.
I would like to set up a simple guard that will redirect to a page if the conditions are not met. Using
ionViewCanEnter appears inadequate, and navigation from this function seems futile.
The scenario seems simple enough: A page requires a user to be authenticated. If navigation is attempted to that page and they are not authenticated, simply redirect them to a login page (that will ultimately redirect them to their desired destination upon successful authentication).
What I have tried
I tried using
ionViewCanEnter to handle the situation. Attempted to redirect using
NavController did not work (nothing happened, no logs or errors thrown). I then read that the previous view should handle a failed navigation, however this is not possible if the destination page is navigated through using a deep link from outside the app. In that case there is no previous view to handle the failed load.
What I think should be the solution
It seems like the most logical solution would be to have vanilla angular-esque route guards: a service that would allow you to simply redirect instead of causing all of this confusion. Am I wrong?
Is there a better solution out there that I have just blatantly missed?