How to parse from a GET request


#1

Hello guys,

i’m doing a login page that gets a token after a GET request, but i have NO IDEA about how to do that.
Documentation is not so clear imho…
I have this provider, i should check the JSON that returns if it have a field, and then return failure or success.

import { Injectable } from '@angular/core';
import { Http } from '@angular/http';
import {Observable} from 'rxjs/Observable';
import 'rxjs/add/operator/map';
 
export class User {
  name: string;
  email: string;
 
  constructor(name: string, email: string) {
    this.name = name;
    this.email = email;
  }
}
 
@Injectable()
export class AuthService {
  currentUser: User;
  data: any;
  result: any;
  http: Http;
 
  public login(credentials) {

    if (credentials.email === null || credentials.password === null) {
      return Observable.throw("Please insert credentials");
    } else {
      return Observable.create(observer => {
        this.data = {api_token: "asdfg", http_id: credentials.email, http_pass: credentials.password};
        this.http.post('http://mywebsite/api/login', JSON.stringify(this.data), {headers: {'Content-Type': 'application/json'}})
        .map(res => res.json())

        let access = (); //i have no idea on how to use this
        observer.next(access);
        observer.complete();
      });
    }
  }
 
  public logout() {
    return Observable.create(observer => {
      this.currentUser = null;
      observer.next(true);
      observer.complete();
    });
  }
}