Skip to main content

withCredentialsInterceptor

The withCredentialsInterceptor is an Angular HTTP interceptor function that automatically adds the withCredentials flag to outgoing HTTP requests.

Import

import { withCredentialsInterceptor } from '@geodome/gdk/common';

Description

This interceptor modifies all outgoing HTTP requests to include credentials (such as cookies) when making cross-origin requests. This is particularly useful when your frontend needs to send authentication cookies to a backend API on a different domain.

Functionality

  • Clones the original request.
  • Sets the withCredentials flag to true on the cloned request.
  • Passes the modified request to the next handler in the interceptor chain.

Usage

To use this interceptor, add it to your Angular application's HTTP_INTERCEPTORS in the app.config.ts file:

import { provideHttpClient, withInterceptors } from '@angular/common/http';
import { withCredentialsInterceptor } from '@geodome/gdk/core';

export const appConfig: ApplicationConfig = {
providers: [
provideHttpClient(withInterceptors([withCredentialsInterceptor])),
// ... other providers
]
};

Key Points

  • Applies to all HTTP requests made through Angular's HttpClient.
  • Enables sending of credentials (like cookies) for cross-origin requests.
  • Useful for maintaining authentication state across different domains.
  • Does not modify the original request, but creates a new one with the added flag.
caution

Ensure your server is configured to handle requests with credentials and has appropriate CORS settings.