I made simple app with Ionic 3/ Angular 5. Just Login and Home page. User is hardcoded (mocked) in AuthService. After login, I do this.nav.push('HomePage') where nav is NavController in loginPage.ts file.
In authService I have this:
return Observable.create(observer => {
let access = (credentials.password === "pw" && credentials.email === "em");
this.currentUser = new User('Test', 'test@mail.com');
observer.next(access);
observer.complete();
});
and:
public getUserInfo(): User {
return this.currentUser;
}
In homePage.ts I have this (auth is authService injected into component):
constructor(private nav: NavController, private auth: AuthService) {
let info = this.auth.getUserInfo();
this.username = info['name'];
this.email = info['email'];
}
After login, my app is on Home page. And if I reload Home page, user gets lost, and this.auth.getUserInfo() returns null...
What is best practice to keep user data (token etc.) in application lifetime and to check if user exists on desired page, if no user, return to login and clear all cache etc, and if user exists, reload that page or do whatever?