How to show other page if user has logged in already?


#1

Hi guys,

I am using SqlStorage for storing user logged in state. Here is my code to check if the user has logged in, then show Homepage rather than LoginPage:

export class MyApp {
  rootPage: Type = LoginPage;

  app: IonicApp;

  constructor(platform: Platform, app: IonicApp) {
	  this.app = app;

	  this.hasLoggedIn().then((hasLoggedIn) => {
		  if (hasLoggedIn === true) {
			  this.openPage(HomePage);
		  }
	  });
  }

  openPage(page) {
	  let nav = this.app.getComponent('nav');
	  nav.setRoot(page);
  }
....

Because this.hasLoggedIn() is a Promise, which gets data from SqlStorage, the LoginPage is shown first, until the data is loaded and this.openPage(HomePage); is called. What it means is the user will see the login screen before the HomePage is shown. Anyone knows how to get around this issue?


#2

I would love to hear other solutions, but in my app, I hide the splash screen after the promise is finished, so users never see the login screen.


#3

Could you please explain a bit more as i dont get what you meant?


#4

Hi,

Maybe you should store a “user_logged_in” flag in the local storage because that would be fetched very fast, so you wouldnt have to wait for the promise from the sql. But thats just one idea, there are probably other ways to do it, let me know if you found a good solution.

Take care.


#5

Yeah, the issue with local storage is that it is subject to being cleared on iOS when the user is low on storage.

So if you store a flag there, then also store it in sqlite. If your quick local storage check doesn’t show anything, then also check for sqlite before assuming the user isnt logged in.


#6

Thats the best option as far as i know now. At the moment, my solution is showing loading screen until it knows whether to show login screen or other screen