Skip to content

Instantly share code, notes, and snippets.

@porfidev
Created June 5, 2017 18:53
Show Gist options
  • Save porfidev/8ddb1edd22362945451d7aa49b563fa0 to your computer and use it in GitHub Desktop.
Save porfidev/8ddb1edd22362945451d7aa49b563fa0 to your computer and use it in GitHub Desktop.
Interceptor (Experimental) para Angular4
import {Injectable} from '@angular/core';
import {BaseRequestOptions, RequestOptions} from '@angular/http';
@Injectable()
export class TokenRequestOptionsService extends BaseRequestOptions {
private token: string;
constructor() {
super();
this.addToken();
}
addToken() {
if (sessionStorage.getItem('token')) {
this.token = sessionStorage.getItem('token');
this.headers.set('Authorization', `Bearer ${this.token}`);
}
}
}
export const tokenRequestOptionsProvider = { provide: RequestOptions, useClass: TokenRequestOptionsService };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment