import { Injectable } from '@angular/core';
import { Http, Headers, Response, RequestOptions } from '@angular/http';
import 'rxjs/add/operator/toPromise';

import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';

@Injectable()
export class DataService {

  private headers = new Headers({ 'Content-Type': 'application/json', 'charset': 'UTF-8' });
  private options = new RequestOptions({ headers: this.headers });

  constructor(private http: Http) { }


  //login service
login(obj: Object): Observable<any> {
    return this.http.post('/login', JSON.stringify(obj), this.options)
      .map((res: Response) => {
        let data = res.json();
        if (Object.keys(data)[0] !== 'message') {
          localStorage.setItem("currentUser", JSON.stringify(data));
        }
        return data;
      }
      );
  }
  logout() {
    localStorage.removeItem('currentUser');
  }


}
