Route guard limitations during asynchronous credential validation
0 reputation · 29 Sept 2021, 16:49 UTC
0 reputation · 29 Sept 2021, 16:49 UTC
Framework7 manages client-side navigation through a router that utilizes lifecycle hooks like beforeEnter and onEnter. While these hooks allow for logic execution, the framework does not provide a native mechanism to pause navigation transitions while an asynchronous authentication check completes.
When implementing least-privilege access, developers often need to verify a session token or check expiration status before allowing a view to render. If the validation logic involves a Promise, the router may proceed with the view transition or initialize the component prematurely, leading to 'flicker' of protected content before the redirect logic is triggered.
How can the Framework7 router handle async-based route guards to prevent the target component from mounting until validation is resolved? Is there a built-in way to cancel a route transition from within a before hook?
34775 reputation · 29 Sept 2021, 17:32 UTC
The Framework7 router executes lifecycle hooks such as beforeEnter and onEnter synchronously. If you start an asynchronous credential check (e.g., fetch or a Promise) inside the hook, the router does not wait for the Promise to resolve. After the hook function returns, the router immediately proceeds with the view transition and mounts the target component.
Because the transition starts before the async validation finishes, the protected component may render briefly. When the validation finally resolves and your code calls router.navigate() to redirect, the user sees a flash of the protected view—a “flicker”.
/auth-check) that does not render sensitive UI.pageInit or onEnter hook, run your asynchronous credential validation.app.views.main.router.navigate() to send the user to the intended protected page on success, or to a login page on failure.If an extra route is undesirable, initialize the protected component in a hidden or loading state. Only after the async check resolves do you set the state to show the real content, otherwise redirect away.
Diagnostic detail needed: Are you using Framework7 as a standalone library, or is it integrated with a framework such as Vue or React? The integration layer may expose additional lifecycle wrappers that could affect the recommended approach.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.