I have a problem to pass a subscribed value to another component.
Structur:
app/
---elements/
------navigation.component.ts
---pages/
------login/
----------login.component.ts
login.component.ts
isAuthenticated: any = false;
onSignin() {
this.authService.signinUser(this.myForm.value)
.subscribe(
data => {
this.isAuthenticated = data;
});
}
this.isAuthenticated
is true if the login was successful.
What is the best way to send the data of isAuthenticated to the navigation.component.ts?
navigation.component.ts
isAuthenticated: any = false;
constructor (private authService: AuthService, private router: Router) {
this.authService.isAuthenticated()
.subscribe(data => {
this.isAuthenticated = data;
});
}
Added: auth.service.ts
isAuthenticated() {
if (this.currentUser != null) {
const token = this.currentUser.token;
const body = JSON.stringify({token: token});
return this.http.post('https://test.com/src/v13n22214', body)
.map((response: Response) => {
const authResponse = response.json().response;
if (authResponse) {
return true;
} else {
return false;
}
});
}