If the user starts the app for the first time the login-page will be shown and the user has to login. If that was succesfull the app redirects the user to the menu-page (and this loads the tabs and so on).
Now, if the user closes the app or sends it to the background and reopens the app, the login-page is shown for a short period of time (less than 1 second) and it redirects again to the menu-page.
How can i prevent the login-page to be shown when the user is already logged in and the user is reopening the app? With ionic 3 that was no problem for me (using setRoot) but now with ionic 4 i face this problem (maybe because of the routes…?)
Note that I’m using a second Guard, named LoginGuard which checks if the ‘AuthGuard’ is true, if so, my ‘LoginGuard’ will redirect to ‘/home’. Remove the LoginGuard in my example if not desired.
My AppComponent is (kind of) totaly empty!
My AuthGuard is handeling the redirection to “root” ( '/') which is subscribed to my ‘AuthService’ keeping track of my authentication.
As you’ve also did read on the other ‘login thread’, because of the initial ‘false’ value of the observable you might indeed see the login page for a split second.
To fix that I’ve implemented my this.splashScreen.hide(); with a 500ms timeout on the only 2 possible landing pages (‘home’ and ‘login’).
I have solved my problems! Now the app starts and when the user is already logged in my dashboard-page will be shown without the Login-page being shown for less than 1 sec.
//Check if user is logged-in; if so the dashboard will be loaded otherwise the login-page
this.authenticationService.authenticationState.subscribe(state => {
if (state) {
console.log("user is logged in");
this.navController.navigateRoot(['menu/tabs/dashboard']);
this.splashScreen.hide();
} else {
console.log("user is NOT logged in");
this.navController.navigateRoot('login');
this.splashScreen.hide();
}
});
When the user is being logged in i set some data into the localstorage. In my authService i look for these data and set the state to true or false. In the app.component.ts i subscribe to this state and switch the root navigation.
still showing login page for 1 second. see authenticationState has initial value as false.
authenticationState = new BehaviorSubject(false);
so it’s going to the login and once authenticationState got true it’s going to the home.
oki thanks.i was fixed that issue using ReplaySubject instead of BehaviorSubject.BehaviorSubject is required initial value.so that’s the case login page shows for 1 second.
Hi shorstmann don’t worry I am here to help you, below is a sample code your help, just add it inside your app.component.ts file don’t forget to vote the answer
One of the things that you have to accept when writing reactive webapps (which all Ionic apps are) is that you cannot know and should not try to influence when things happen. That’s sort of baked into the word “reactive” in the first place. You put things into certain states, wait for triggers to happen, and then react.
In this case, the process of discovering whether a user has already been authenticated is one of those things with unpredictable timing. Instead of trying to eliminate that “1 second”, I would suggest instead putting the app into a state for however long it takes that makes sense for either case (authenticated or not).
The most straightforward way I would think to try would be to delay hiding the splash screen until inside your subscription. If that isn’t to your liking, how about creating your own “splash page”, navigate to that immediately, and then later on to /tabs or login as appropriate?
As rapropos did made it clear, whatever you do, one way around, you’ll have to give the app time to initialize. So the timeout / delay solution should be a proper solution. Just don’t forget that it will only work ‘correct’ when running on an device and not in your browser. (no splashscreen in browser!)