Advertisement
Guest User

Untitled

a guest
Aug 17th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1. import {Injectable} from '@angular/core';
  2. import {Http, XHRBackend, RequestOptions, Request, RequestOptionsArgs, Response, Headers} from '@angular/http';
  3. import {Observable} from 'rxjs/Observable';
  4. import 'rxjs/add/operator/map';
  5. import 'rxjs/add/operator/catch';
  6. import {Subject} from 'rxjs/Subject';
  7.  
  8. @Injectable()
  9. export class customHttp extends Http {
  10. tokenexpired: Subject<boolean> = new Subject<boolean>();
  11.  
  12. constructor(backend: XHRBackend, options: RequestOptions) {
  13.  
  14. super(backend, options);
  15. console.log('In constructor');
  16. let token = localStorage.getItem('auth_token'); // your custom token getter function here
  17. options.headers.set('Authorization', `Bearer ${token}`);
  18. this.tokenexpired.next(false);
  19. console.log('token is');
  20. console.log(this.tokenexpired);
  21. }
  22.  
  23. request(url: string | Request, options?: RequestOptionsArgs): Observable<Response> {
  24. console.log('In service');
  25. let token = localStorage.getItem('auth_token');
  26. if (typeof url === 'string') { // meaning we have to add the token to the options, not in url
  27. if (!options) {
  28. // let's make option object
  29. options = {headers: new Headers()};
  30. }
  31. options.headers.set('Authorization', `Bearer ${token}`);
  32. } else {
  33. // we have to add the token to the url object
  34. url.headers.set('Authorization', `Bearer ${token}`);
  35. }
  36. return super.request(url, options).catch(this.catchAuthError(this));
  37. }
  38.  
  39. post(url: string, body: any, options?: RequestOptionsArgs): Observable<any> {
  40. console.log('Before the request...');
  41. return super.post(url, body, options)
  42. .catch((err: any): any => {
  43. if (err.status === 400 || err.status === 422) {
  44. //FIRE NOTIFICATION IN THIS POINT FOR EXAMPLE.
  45. return Observable.throw(err);
  46. } else {
  47.  
  48.  
  49. }
  50. })
  51. .finally(() => {
  52. console.log('After the request...');
  53. });
  54. }
  55. private catchAuthError(self: customHttp) {
  56. // we have to pass HttpService's own instance here as `self`
  57. return (res: Response) => {
  58. console.log(res);
  59. if (res.status === 401 || res.status === 403) {
  60. // if not authenticated
  61. console.log(res);
  62. this.deletetoken();
  63. }
  64. return Observable.throw(res);
  65. };
  66. }
  67.  
  68. private deletetoken() {
  69. localStorage.removeItem('token');
  70. this.tokenexpired.next(true);
  71. }
  72. }
  73.  
  74. constructor(private http: customHttp) {
  75. this.number = this.generate();
  76. }
  77.  
  78. register(data: any): Promise<Response> {
  79. let datatosend = JSON.stringify({
  80. email: data.email,
  81. password: '12345',
  82. user: {cnic: data.cnic, phone: this.number}
  83. });
  84. return this.http
  85. .post(authUrls.register,datatosend , {headers: this.headers})
  86. .toPromise()
  87. .then(response => {
  88. this._profileChange(response);
  89. this._serrviceLogin();
  90. return response;
  91. })
  92. .catch(this.handleError);
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement