Hy!
I have a really intresting problem. I’m sure I did something wrong but I really don’t know what would be the “good” solution, the best way to handle this edge-case.
After login I store user’s token in localstorage and in every provider’s constructor I read it into a promise:
this._headers = this.storage.get('token').then((token) => { return new Headers({ "Content-Type": "application/json", "Authorization": "Bearer " + token, }); });
And I use it in every request. Maybe a relevant information is that I define the providers in app.module.ts to be singletons. (I’m not sure if it is a good practice I still learn ionic 2).
Now the problem is that if the user logs out and a new user logs in the token don’t change until I restart the app.
I guess that because of the provider already exists it’s constructor isn’t called at login so the token don’t update.
I managed to solve with an event. On login I publish an event and in every provider after this event I read again the token, BUT I’m sure that this isn’t a good practice.
My real question is that how can I solve this issue?
Thank you very much.