question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

bug(vue): redirecting in a navigation guard after going back does not show the correct view

See original GitHub issue

Prerequisites

Ionic Framework Version

  • v4.x
  • v5.x
  • v6.x
  • Nightly

Current Behavior

Relevant context:

I have an app with a home page, a settings page, and a login page. The login page navigates to the home page after login, or back to the route it was redirected from. The home page has a button to navigate to the settings page and the settings page has a back button.

There is a beforeEach navigation guard that checks if the user is logged in; if they are not, it redirects to the login page. If they are logged in, it checks if the destination is the login page; if so, it redirects to the home page.

Now for the problem:

When I try to go to the settings page directly (by entering the URL) while logged out, I am redirected to the login page as expected. After logging in, I am taken to the settings page, again as expected.

The back button does not work correctly now. The initial “back” navigation is to the login page, so the navigation guard redirects to the home page. However, the view shown is the login page. The URL is for the home page. The same happens when using router.back().

It seems to be caused at least in part by the way Ionic’s view stack works.

I am now using replace instead of push (which was actually “incorrect”), even if this does unfortunately mean I no longer get a navigation animation when navigating from the login page. I understand that this was caused by a “bad” implementation of mine, but this situation could arise in other contexts.

Expected Behavior

I would expect Ionic to wait until the navigation was confirmed and then check if the final route was the same as the one “back” from the current view. If the route was the same, Ionic would pop the current view as normal; if it was not (i.e., a navigation guard had caused a redirect), Ionic would create the correct view for the new route, render it beneath the current page, rewrite the page stack to include just that view and the current one, and then navigate to that view as if going back.

Steps to Reproduce

  1. Setup
    1. Create an Ionic Vue app with three pages, “home”, “settings”, and “login”.
    2. Add a button/link from the home page to the settings page.
    3. Add a back button from the settings page, default route being for the home page.
    4. Put a button on the login page that pushes to the settings page and stores a flag in global state (e.g. via a Vue composable).
    5. Make a navigation guard that checks the flag and (a) redirects to the login page if it is not set and (b) redirects to the home page if it is set and the current destination is the login page.
  2. Enter the URL for the settings page directly. You should be redirected immediately to the login page.
  3. Click the button to go to the settings page.
  4. Click the back button. The URL should change to the home page but the view shown will be the login page.

Code Reproduction URL

No response

Ionic Info

Ionic:

   Ionic CLI       : 6.16.3 (C:\Users\Darryl\AppData\Roaming\npm\node_modules\@ionic\cli)
   Ionic Framework : @ionic/vue 6.2.6

Utility:

   cordova-res : not installed globally
   native-run  : not installed globally

System:

   NodeJS : v18.4.0 (C:\Program Files\nodejs\node.exe)
   npm    : 8.12.1
   OS     : Windows 10

Additional Information

No response

Issue Analytics

  • State:closed
  • Created a year ago
  • Reactions:1
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
darrylnoakescommented, Oct 27, 2022

Thanks, that clarifies it sufficiently.

0reactions
ionitron-bot[bot]commented, Nov 26, 2022

Thanks for the issue! This issue is being locked to prevent comments that are not relevant to the original issue. If this is still an issue with the latest version of Ionic, please create a new issue and ensure the template is fully filled out.

Read more comments on GitHub >

github_iconTop Results From Across the Web

vue-router — Uncaught (in promise) Error: Redirected from ...
I ran into a case that <router-link> was working fine, but router.push was complaining. (I think router-link internally suppresses such errors.) ...
Read more >
Waiting for the result of a Navigation - Vue Router
A navigation guard redirects somewhere else by returning a new location (e.g. return '/login' ). A navigation guard throws an Error .
Read more >
The Complete Guide to Vue.js User Authentication with Auth0
See LogoutOptions for more details. Here, you pass the returnTo option to specify the URL where Auth0 should redirect your users after they...
Read more >
Complete Vue Router 4 Guide: Basics, Programmatic Routing ...
When the nested RouterView is not displaying any component, we do not want to see the outer DIV. Any idea how to hide...
Read more >
Understand Routing in Vue.js With Examples | by SaidHayani
Nested routers · Named view · Redirect and Alias · Navigation Guard · Router instance. Now you can browse to any components by...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found