-
Notifications
You must be signed in to change notification settings - Fork 46
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Question about getHeaders #57
Comments
Hello. |
Hi! Usually i'm implementing AuthGuard resource class which blocks all requests until import { Request, Response } from '@angular/http';
import { Resource } from 'ng2-resource-rest';
import { Observable, Subscriber } from 'rxjs';
import { AppConfigResource } from './appConfig.resource';
import { IAppConfigEvent, TAppConfigEvent } from '../interfaces/common.interfaces';
import { AuthService, ConfigService } from '../services/index';
export class AuthGuardResource extends Resource {
private deferredQ: Subscriber<any>[] = [];
private configListenerSet: boolean = false;
getHeaders(): any {
let headers = super.getHeaders();
headers = AuthService.instance.extendHeaders(headers);
return headers;
}
responseInterceptor(observable: Observable<any>, request?: Request): Observable<any> {
// Checking is config has been loaded
if (ConfigService.instance.isLoaded || this instanceof AppConfigResource) {
return Observable.create((subscriber: Subscriber<any>) => {
observable.subscribe(
(res: Response) => {
if (res.headers) {
let newToken: string = res.headers.get('X-C3-AUTH-TOKEN');
if (newToken) {
AuthService.instance.token = newToken;
}
}
subscriber.next((<any>res)._body ? res.json() : null);
},
(error: Response) => {
if (error.status === 401) {
AuthService.instance.token = null;
}
//console.warn('BaseResource request error', error, request);
subscriber.error(error);
},
() => subscriber.complete()
);
});
}
// Creating deferred observable
let retObservable = Observable.create((observer: Subscriber<any>) => {
this.deferredQ.push(observer);
}).flatMap(() => this.responseInterceptor(observable));
this.startConfigListener();
return retObservable;
}
private startConfigListener() {
if (this.configListenerSet) {
return;
}
this.configListenerSet = true;
let subscr = ConfigService.instance.eventBus
.filter((event: IAppConfigEvent) => event.type === TAppConfigEvent.loadSuccess)
.subscribe(
() => {
if (ConfigService.instance.isLoaded) {
subscr.unsubscribe();
this.executeDeferred();
}
}
);
}
private executeDeferred() {
let deferredQ = this.deferredQ;
this.deferredQ = [];
deferredQ.forEach((deferred) => {
deferred.next();
deferred.complete();
});
}
} |
Sorry @troyanskiy, my mistake. 😞 Thanks for your Answare. |
Hello.
I'm trying to implement API with OAuth. i made a service to handle that and works propertly but i want to used it in the resources. I created a
OAuthGuardResource
to extends of it all other Resourcesand i have the
UserResource
The thing is when the resource make the
query
, the methodgetHeaders
is called two times.I dont know if I'm doing something wrong.
Thanks
The text was updated successfully, but these errors were encountered: