Created
June 24, 2017 14:20
-
-
Save NarHakobyan/18926e032e6d3c4be5754d70bcfccae8 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { BaseRequestOptions, RequestOptions } from '@angular/http'; | |
import { AuthService } from './auth.service'; | |
@Injectable() | |
export class DefaultRequestOptions extends BaseRequestOptions { | |
constructor(private authService: AuthService) { | |
super(); | |
// Set the default 'Authorization' header | |
this.headers.set('Authorization', `Bearer ${authService.token}`); | |
// Update: You need to subscribe to an observable that will update the JWT. Otherwise, it will run only once. | |
// Something like: | |
// AuthService.user.subscribe(user => this.headers.set('Authorization', `Bearer ${user.token}`);) | |
} | |
} | |
export const requestOptionsProvider = { provide: RequestOptions, useClass: DefaultRequestOptions }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment