I have an ionic 4 app that is storing information about the user natively. When the user opens the app then if his information is there then I want him to be sent to the Home Page. If he is not then I want him to be routed to the Login Page, pretty normal.
The problem that I am running into is that for about 500ms you will see the Login Page and then upon reading the information then it will redirect you to the home page.
What I would like is for the application to WAIT On the response from the storage before it directs you to the Login Page or the Home Page.
App.component.ts
public initializeApp(): void {
this.platform.ready().then((): void => {
this.statusBar.styleDefault();
this.splashScreen.hide();
this.authService.authState.subscribe((state: boolean): void => {
if (state) {
this.router.navigate(['home']);
} else {
this.router.navigate(['login-register']);
}
});
this.permissionService.requestCameraPermission();
});
}
AuthService.service.ts
public readonly authState: BehaviorSubject<boolean> = new BehaviorSubject<boolean>(false)
private _ifLoggedIn(): void {
// just sees if his info is saved, if so then he has been logged in
this.userService.getUserInfo().then((response: UserData): void => {
if (response) {
this.authState.next(true);
}
});
}
public isAuthenticated(): boolean {
return this.authState.value;
}
AuthGuard
export class AuthGuardService {
private authService: AuthService;
public constructor(authService: AuthService) {
this.authService = authService;
}
public canActivate(): boolean {
return this.authService.isAuthenticated();
}
}
app-routing.module.ts
const routes: Routes = [
{
path: '',
redirectTo: 'login-register',
pathMatch: 'full',
},
{
path: 'login-register',
loadChildren: () => import('../pages/login-register/login-register.module').then(m => m.LoginRegisterPageModule),
},
{
path: 'home',
loadChildren: () => import('../pages/home/home.module').then(m => m.HomePageModule),
canActivate: [AuthGuardService],
},
];
if there is anything other information you need let me know. Thanks guys!