Hi ! 馃憢
I'm looking for a way to make a request with credentials (withCredentials: true).
Here I can't make a request to my backend because the request doesn't contain cookies header.
Of course, I would like to continue using the EntityCollectionServiceBase to process default requests (e.g. getAll).
I couldn't find an option for that. 馃槙
Thanks for your help
Of course, an alternative is to use an Interceptor:
@Injectable()
export class CustomInterceptor implements HttpInterceptor {
public intercept(request: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
const request = request.clone({
withCredentials: true
});
return next.handle(request);
}
}
Is it the best practice ?
@kapik there is no best practice for this case! CustomInterceptor is also a good way to implement that. I would recommend to place and register your CustomInterceptor where your EntityCollectionServiceBase is located and to make sure to set the credentials on particular url when they is a requirement!
...and yes, there is an alternate way! You could use a Custom DataService (https://ngrx.io/guide/data/entity-dataservice#custom-entitydataservice). With that you could set the withCredentials property on only getAll() request.
Example:
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import {
EntityCollectionDataService,
DefaultDataService,
HttpUrlGenerator,
QueryParams
} from '@ngrx/data';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';
import { Hero } from '../../core';
@Injectable()
export class YourDataService extends DefaultDataService<SomeModel> {
constructor(http: HttpClient, httpUrlGenerator: HttpUrlGenerator) {
super('MyEntityName', http, httpUrlGenerator);
}
getAll(): Observable<SomeModel[]> {
const httpOptions = {
headers: { 'Content-Type': 'application/json' },
params: {withCredentials: true}
};
const urlPath = this.httpUrlGenerator.collectionResource('MyEntityName', '');
return this.http.get(urlPath, httpOptions);
}
}
Most helpful comment
@kapik there is no best practice for this case! CustomInterceptor is also a good way to implement that. I would recommend to place and register your CustomInterceptor where your EntityCollectionServiceBase is located and to make sure to set the credentials on particular url when they is a requirement!
...and yes, there is an alternate way! You could use a Custom DataService (https://ngrx.io/guide/data/entity-dataservice#custom-entitydataservice). With that you could set the withCredentials property on only getAll() request.
Example: