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!)