Created
June 9, 2017 07:43
-
-
Save argentinaluiz/7de5328f81d68a6e5d229ca735e2d1f5 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
//// | |
providers:[ | |
{provide: XHRBackend, useClass: DefaultXHRBackend}, | |
// |
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 { | |
BrowserXhr, | |
Request, Response, | |
ResponseOptions, | |
XHRBackend, | |
XHRConnection, | |
XSRFStrategy | |
} from '@angular/http'; | |
import 'rxjs/add/operator/map'; | |
@Injectable() | |
export class DefaultXHRBackend extends XHRBackend { | |
constructor(browserXHR: BrowserXhr, | |
baseResponseOptions: ResponseOptions, | |
xsrfStrategy: XSRFStrategy) { | |
super(browserXHR, baseResponseOptions, xsrfStrategy); | |
} | |
createConnection(request: Request): XHRConnection { | |
let xhrConnection = super.createConnection(request); | |
xhrConnection.response = xhrConnection | |
.response | |
.map((response) => { | |
this.tokenSetter(response); | |
return response; | |
}); | |
return xhrConnection; | |
} | |
tokenSetter(response: Response) { | |
if (response.headers.has('Authorization')) { | |
let authorization = response.headers.get('Authorization'); | |
let token = authorization.replace('Bearer ', ''); | |
//armazenar token renovado no local storage | |
//use aqui o window.localstorage para atualizar o token | |
//para não precisar de injeção de dependência do serviço de armazenamento | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment