Advertisement
Guest User

Untitled

a guest
May 20th, 2019
198
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.33 KB | None | 0 0
  1. import { Injectable } from "@angular/core";
  2. // import { Http, Headers, HttpModule, Response } from "@angular/http";
  3. import { HttpClient } from '@angular/common/http';
  4.  
  5. import { Observable, throwError } from "rxjs";
  6. import { catchError, map, tap } from "rxjs/operators";
  7.  
  8. import { User } from "./user.model";
  9. import { Config } from "../config";
  10.  
  11. @Injectable()
  12. export class UserService {
  13. constructor(private http: HttpClient) { }
  14.  
  15. register(user: User) {
  16. if (!user.email || !user.password) {
  17. return throwError("Please provide both an email address and password.");
  18. }
  19.  
  20. return this.http.post(
  21. Config.apiUrl + "user/" + Config.appKey,
  22. JSON.stringify({
  23. username: user.email,
  24. email: user.email,
  25. password: user.password
  26. }),
  27. //{ headers: this.getCommonHeaders() }
  28. ).pipe(
  29. catchError(this.handleErrors)
  30. );
  31. }
  32.  
  33. getCommonHeaders() {
  34. let headers = new Headers();
  35. headers.append("Content-Type", "application/json");
  36. headers.append("Authorization", Config.authHeader);
  37. return headers;
  38. }
  39.  
  40. handleErrors(error: Response) {
  41. console.log(JSON.stringify(error.json()));
  42. return Observable.throw(error);
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement