Hello!
I’m developing ionic/angular/cordova app. And I have some issues with stucture.
So:
I have side menu with username and user image, and method, that get this info:
export class AppComponent {
public appPages = [
{
title: 'test',
url: '/test'
},
];
name: any;
avatar: any;
surname: any;
constructor(
...
private platform: Platform,
private authService: AuthService,
...
) {
this.initializeApp();
}
initializeApp() {
this.platform.ready().then(() => {
...
this.getUser()
});
}
getUser() {
this.authService.user().subscribe(res => {
this.avatar = res.avatar
this.name = res.firstname
this.surname = res.lastname
})
}
}
So when user is logged, I can get user info from authService, with token.
But!
When, I’m trying to get this info without token (user not logged in), I get error.
So, I need to add some check:
if(this.authService.isLoggedIn) {
this.authService.user().subscribe(res => {
this.avatar = res.avatar
this.name = res.firstname
this.surname = res.lastname
})
}
And authService.isLoggedIn is boolean:
isLoggedIn = false;
So, first the problem is:
When I’m adding check, function is not working. Why?
And the second problem:
When user logging in, I need to call this method. How can I do it?
Maybe there is normal way to use app.component?