My angular client is separate from the backend, and I enabled cors on the backend, everything works fine, except that my authentication failed because the cookie was not added to the requests.
After searching the Internet, I found that I had to set {withCredentials : true} for each HTTP request. I managed to do this on one request, and it works, but not on all requests.
I tried using BrowserXhr. How to send a βCookieβ in the request header for all requests in Angular2? but it does not work and it is also deprecated by afaik.
I also tried RequestOptions, but that did not work.
What can I do to set {withCredentials: true} for every HTTP request?
Later Edit:
@Injectable() export class ConfigInterceptor implements HttpInterceptor { constructor(private csrfService: CSRFService) { } intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { let token = this.csrfService.getCSRF() as string; const credentialsReq = req.clone({withCredentials : true, setHeaders: { "X-XSRF-TOKEN": token } }); return next.handle(credentialsReq); } }
source share