I'm trying to return information from my api but I don't understand how to correctly use subscribe. With an array I return an empty array from my service and push values into it as I get them.
How would I correctly return just a single value variable in app-component ts. Right now If I do a alert(JSON.stringify(authenticated))
it just gives me {"_isUnsubscribed":false,"_subscriptions":[{"isUnsubscribed":false}]}
app-component ts
checkAuthentication(){
var authenticated = this._authService.getAuthenication();
}
authentication Service
getAuthenication() {
return this._http.get('auth.php').subscribe(res => {
if (res.json().authenticated === true){
return true;
}
return false;
});
}
auth.php
<?
session_start();
$authenticated = ( isset($_SESSION["authenticated"]) ? true : false );
$post_data = json_encode(array(authenticated => $authenticated));
echo $post_data;
?>
change
return this._http.get('auth.php').subscribe(res => {
to
return this._http.get('auth.php').map(res => {
and call it like
this._authService.getAuthenication()
.subscribe(value => this.authenticated = value);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With