Goldbones
Goldbones

Reputation: 1457

Ionic 2 - Store user credentials (local storage vs nav params)

I´m developing an Ionic 2 app. When I make login, I could store user credentials on SQLStorage, LocalStorage or transfer it as parameter on NavParam.

Example:

onLogin(){
this.nav.setRoot(MainPageComponent,user);
}

vs

let storage = new Storage(SqlStorage);
storage.set('name', 'Max');

What is the main advantage of storage over passing as parameter?

Upvotes: 1

Views: 995

Answers (2)

sebaferreras
sebaferreras

Reputation: 44659

What is the main advantage of storage over passing as parameter?

If you use that information just in one page or two, you can send it as parameter. But the problem is that you will probably use the name in more than one place (maybe in a settings page, or in the side menu of the app, and so on), so storing that information somewhere would be a good idea so you can get it back when you need to.

Also, if you get that information in one page, and you want to show it in another one, but those pages are not connected directly in the workflow of your app, you would need to pass it through some other pages just to get it where you want to show it. So again, storing that data seems to be a better approach.

Please notice that if you use SQLStorage or LocalStorage, you should set also an expiration date to force the user to log in again after a given period of time.

Upvotes: 1

gsthina
gsthina

Reputation: 1100

Navparams is used for transferring the data between pages say, you have to open a particular item's details, you need to click on an Item and the parameters will be sent using Navprams to the itemDescription page. You prefer localstorage.setItem('userId', $userId), because storing locally will solve the unwanted sign-in every time you open the app. If you store in your local storage, then on every app on load, we can check the existence of the local variable.

if(localstorage.getItem('userId')==null){
    this.signin();
}
else{
    this.appOpen();
}

This one can help you!

Upvotes: 1

Related Questions