Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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;
?>
like image 955
ClickThisNick Avatar asked Feb 20 '16 14:02

ClickThisNick


1 Answers

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);
like image 70
Günter Zöchbauer Avatar answered Nov 15 '22 12:11

Günter Zöchbauer