Advertisement
Guest User

Untitled

a guest
Apr 16th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. @Injectable()
  2. export class AuthenticationData {
  3.  
  4. authenticationEndPoint: string = "url";
  5.  
  6. constructor(public http: Http) {
  7. super();
  8. }
  9.  
  10. //login
  11. loginUser(username: string, password: string): Observable<any> {
  12. let headers = new Headers();
  13. headers.append('content-type', 'application/json');
  14. let body = '';
  15. let options = new RequestOptions({ headers: headers });
  16. let url = this.authenticationEndPoint + encodeURI(username) + '&password=' + encodeURI(password);
  17.  
  18. return this.http.post(url, body, options)
  19. .map(this.extractData)
  20. .catch(this.handleError);
  21. }
  22.  
  23.  
  24. //to extract data
  25. private extractData(res: Response) {
  26. let body = res.json();
  27. return body || {};
  28. }
  29.  
  30. //to handle error
  31. private handleError(error: Response | any) {
  32. let errMsg: string;
  33. if (error instanceof Response) {
  34. const body = error.json() || '';
  35. const err = body.error || JSON.stringify(body);
  36. errMsg = `${error.status} - ${error.statusText || ''} ${err}`;
  37. } else {
  38. errMsg = error.message ? error.message : error.toString();
  39. }
  40. console.error(errMsg);
  41. return Observable.throw(errMsg);
  42. }
  43. }
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement