ClickThisNick
ClickThisNick

Reputation: 5250

TypeScript Angular 2 Responses Subscribing

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;
?>

Upvotes: 5

Views: 6526

Answers (1)

G&#252;nter Z&#246;chbauer
G&#252;nter Z&#246;chbauer

Reputation: 657338

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);

Upvotes: 7

Related Questions