-
Notifications
You must be signed in to change notification settings - Fork 13.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Missing CanActivate/Resolve Functionality in Ionic2 #6148
Comments
Any update on this? Not having resolve is the main reason why I haven't migrated to ionic2 yet. I can work around the issue, but would rather wait if it's going to land soon. |
I would love to see this in the ionic code as well! |
I checked recently with
no progress so far :( |
I agree, it would be cool to have this feature. |
This has been driving me nuts the last few days, so I've gone and implemented this myself. Since the NavController uses transitions directly instead of the relying on Angular2's (relatively new) beta-router, I simply delayed the transition to wait until both the promise returned by the 'entering-view component' using 'ionCanActivate(enteringView: ViewController, leavingView: ViewController)' and the 'leaving-view component' using 'ionCanDeactivate(enteringView: ViewController, leavingView: ViewController)' both have been resolved or cancel the transition if any Promise fails. See my modifications, might create a Pull-Request... Now I can finally implement authentication without relying on workarounds with NavParams or the LoadingController. :) |
I need some feedback from the ionic crew: I'm asking, because I'd like to improve the current implementation to reject the navigation promise (eg. returned by nav.setRoot()) upon rejection of an ionCan... promise. For this I'd like to change the current nav-controller to actually use promise-chaining instead of using the deferred-promise anti-pattern, so I don't have to pass done (and failed) callbacks around... |
Hey @Hufschmidt and @melkir have a look on my SO Question which got some sound workarounds. Maybe its also working for you @melkir: https://stackoverflow.com/questions/36569179/get-data-async-before-a-page-gets-rendered Upvotes are appreciated 🌴 |
@Hufschmidt We are currently doing alot of work around navigation. While it is not a rewrite of NavController, as NavController works perfectly for what it is meant to do, we are adding some very cool functionality that should solve this issue. Thanks! |
@jgw96 Thanks for the feedback. With this info I'll probably not put any more work-hours into improving my current changes to the NavController and rather wait on the awesome things to come, keep up the good work. 😃 @ataraxus Yes, those where the workarounds I was talking about, thanks for posting the link though, good idea. 😃 sebaferreras solutiuon: Thomas Withaars solution: Comparatively I'd say the later solution (while not the accepted one) is the lesser of both "evils". |
Is there an update on this now that we have RC0? Thanks! |
RC0 supports ionViewCanLeave and ionViewCanEnter. |
@manucorporat It says so in the changelog, however the NavController docs says nothing about how are they used, do they expect true or false? do they expect a promise to resolve or reject? |
I've been playing with it and returning false is the way to stop navigation, however i don't see how to trigger a redirect other than using import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Page2 } from '../page2/page2.ts';
@Component({
selector: 'page-page1',
templateUrl: 'page1.html'
})
export class Page1 {
constructor(public navCtrl: NavController) { }
ionViewCanEnter() {
console.log('Can enter.');
setTimeout(() => {
this.navCtrl.setRoot(Page2)
}, 0);
return false;
}
} |
Ok not so much, that only works at the start, if i just return false in a page that's supposed to be navigated after the root component is been set, it breaks. |
Is using |
This is just awful. I can't conceive how the authentication use case was not fully fleshed out when designing the navigation controller. |
Is still no way to control authentication? |
@Luchillo, @mohammadshamma have a look at the navcontroller |
Indeed, thanks @ataraxus for the link. Seems the documentation contains very clear examples and explanations now. Haven't had time to actually test this yet, but it's clearly written so hopefully reproducing this will be straightforward and simple. 👍 |
Hi, I jump in the discussion as I am also facing the same issue. I am trying to secure pages of my app but if we compare the ionViewCanEnter and canActivate they don't have the same signature which quite annoying if we want to achieve the same goal. |
@ataraxus My criticism is for the fact that there is no cross cutting way of doing the check for every page. My understanding is that I might have to define the |
@mohammadshamma I think this, as it is always a trade of... either you whitelist or you blacklist. If you dont want to duplicate your auth code, just create a class SecurePage with your ionViewCanEnter and subclass all pages need to be secured. I know its not as common in JS to subclass, I need to get used to it too ;) |
Any updates? |
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. |
Short description of the problem:
Need to resolve data before
@Page
gets rendered.What behavior are you expecting?
IMHO: Ionics View callbacks
onPage*
to respect/handle returned promises/observables.Detailed description of the problem:
Hello,
I'm opening this issue after trying to find a solution/help in the forum, SO and slack. Till now there are good hints for how to work around this issue, but no definitive solution.
As for now the decorator
@CanActivate
of Angular2 gets not called within Ionic2. Which opens the question what is the correct way to get data async before a Page gets rendered? This issue was posted on the forums, but didn't get any "solution".Apparently Ionic2 does something with the @CanActivate decorator, but its not documented and i can't figure out what it does exactly.
Nevertheless this guy points out one should use Ionic2 View States instead. His example looks like this:
Which looks like he is expecting Ionic to consider the returned promise, but a quick glance at Ionics sources reveals (at least I think so) that the returned value is ignored. Hence there is no guarantee that the promise gets resolved before the page gets rendered. During a discussion on #ionic-v2 I created an example with onPage* and how it does not perform as needed/expected.
Many people suggested to resolve the data before navigating to the page, which burdens the knowledge which data is needed for the page on the callee. This breaks the nice encapsulation of the Angular2 Component in my opinion.
I'm using
The text was updated successfully, but these errors were encountered: