Guest User

Untitled

a guest
May 22nd, 2018
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. import { Injectable } from '@angular/core';
  2. import { RequestOptions } from '@angular/http';
  3.  
  4. @Injectable()
  5.  
  6. export class SharedService {
  7. headers;
  8. url = 'http://localhost:8000/api/';
  9.  
  10. constructor() {
  11. this.headers = '';
  12. }
  13.  
  14. setHead(token: string){
  15. this.headers = new Headers({
  16. 'Authorization': 'Bearer ' + token,
  17. 'Content-Type': 'application/json'
  18. });
  19. }
  20.  
  21. getHead(){
  22. return new RequestOptions({ headers: this.headers });
  23. }
  24.  
  25. }
  26.  
  27. import { SharedService } from './shared.service';
  28.  
  29. @NgModule({
  30. bootstrap: [AppComponent],
  31. providers: [
  32. ...
  33. SharedService,
  34. ],
  35. export class AppModule {
  36. }
  37.  
  38. constructor(
  39. private authService: NbAuthService,
  40. private sharedService: SharedService
  41. ) {
  42. this.authService.onTokenChange()
  43. .subscribe((token: NbAuthJWTToken) => {
  44. if(token.isValid()){
  45. this.sharedService.setHead(token.toString());
  46. }
  47. });
  48. }
  49.  
  50. body : null
  51. headers : Headers
  52. __proto__ : Headers
  53. method : null
  54. params : undefined
  55. responseType : null
  56. search : (...)
  57. url : null
  58. withCredentials : null
  59. __proto__ : Object
Add Comment
Please, Sign In to add comment