title: Angular 4 - setting withCredentials on every request - cors cookie
body: <p>My angular client is separated from the backend and I have enabled cors on the backend, everything works fine except the fact that my authentication fails because the cookie is not added to requests.</p>
  <p>After searching online I found that I should set <code>{withCredentials : true}</code> on every http request. I managed to do it on a single request and it works, but not on all the requests.</p>
  <p>I tried using BrowserXhr <a href="https://stackoverflow.com/questions/35602866/how-to-send-cookie-in-request-header-for-all-the-requests-in-angular2">How to send &quot;Cookie&quot; in request header for all the requests in Angular2?</a> but it doesn't work and it's also deprecated afaik.</p>
  <p>I also tried RequestOptions but it didn't work.</p>
  <p>What can I do to set <code>{withCredentials: true}</code> on every http request?</p>
  <p>Later Edit:</p>
  <pre><code>@Injectable()
  export class ConfigInterceptor implements HttpInterceptor {
  
    constructor(private csrfService: CSRFService) {
  
    }
  
    intercept(req: HttpRequest&lt;any&gt;, next: HttpHandler): Observable&lt;HttpEvent&lt;any&gt;&gt; {
      let token = this.csrfService.getCSRF() as string;
      const credentialsReq = req.clone({withCredentials : true, setHeaders: { &quot;X-XSRF-TOKEN&quot;: token } });
      return next.handle(credentialsReq);
    }
  }
  </code></pre>