[ ] bug report => search for a similar issue before submitting
[ ] feature request
[ X] question
import { Injector } from '@angular/core';
import { Http, XHRBackend, RequestOptions } from '@angular/http';
import { JhiEventManager, JhiInterceptableHttp } from 'ng-jhipster';
import { AuthInterceptor } from './auth.interceptor';
import { LocalStorageService, SessionStorageService } from 'ng2-webstorage';
import { AuthExpiredInterceptor } from './auth-expired.interceptor';
import { ErrorHandlerInterceptor } from './errorhandler.interceptor';
import { NotificationInterceptor } from './notification.interceptor';
import { NgProgressService } from 'ngx-progressbar';
export function interceptableFactory(
backend: XHRBackend,
defaultOptions: RequestOptions,
localStorage: LocalStorageService,
sessionStorage: SessionStorageService,
injector: Injector,
eventManager: JhiEventManager,
progressService: NgProgressService
) {
return new JhiInterceptableHttp(
backend,
defaultOptions,
[
new AuthInterceptor(localStorage, sessionStorage, progressService),
new AuthExpiredInterceptor(injector),
// Other interceptors can be added here
new ErrorHandlerInterceptor(eventManager),
new NotificationInterceptor(injector)
]
);
};
export function customHttpProvider() {
return {
provide: Http,
useFactory: interceptableFactory,
deps: [
XHRBackend,
RequestOptions,
LocalStorageService,
SessionStorageService,
Injector,
JhiEventManager,
NgProgressService,
]
};
};
import { Observable } from 'rxjs/Observable';
import { RequestOptionsArgs, Response } from '@angular/http';
import { LocalStorageService, SessionStorageService } from 'ng2-webstorage';
import { JhiHttpInterceptor } from 'ng-jhipster';
import { NgProgressService } from 'ngx-progressbar';
export class AuthInterceptor extends JhiHttpInterceptor {
constructor(
private localStorage: LocalStorageService,
private sessionStorage: SessionStorageService,
private progressService: NgProgressService
) {
super();
}
requestIntercept(options?: RequestOptionsArgs): RequestOptionsArgs {
console.log(Date.now);
this.progressService.start();
const token = this.localStorage.retrieve('authenticationToken') || this.sessionStorage.retrieve('authenticationToken');
if (!!token) {
options.headers.append('Authorization', 'Bearer ' + token);
}
return options;
}
responseIntercept(observable: Observable<Response>): Observable<Response> {
return observable.finally(() => {
console.log(Date.now);
this.progressService.done();
}); // by pass
}
}