Skip to content

Instantly share code, notes, and snippets.

@NarHakobyan
Created June 24, 2017 14:20
Show Gist options
  • Save NarHakobyan/18926e032e6d3c4be5754d70bcfccae8 to your computer and use it in GitHub Desktop.
Save NarHakobyan/18926e032e6d3c4be5754d70bcfccae8 to your computer and use it in GitHub Desktop.
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