38 lines
1.2 KiB
Plaintext
38 lines
1.2 KiB
Plaintext
import { HttpInterceptorFn } from '@angular/common/http';
|
|
|
|
export const credentialsInterceptor: HttpInterceptorFn = (req, next) => {
|
|
// Clone the request to add the withCredentials property
|
|
const clonedRequest = req.clone({
|
|
withCredentials: true
|
|
});
|
|
|
|
return next(clonedRequest);
|
|
};
|
|
|
|
typescriptimport { provideHttpClient, withInterceptors } from '@angular/common/http';
|
|
import { credentialsInterceptor } from './credentials.interceptor';
|
|
|
|
export const appConfig: ApplicationConfig = {
|
|
providers: [
|
|
provideHttpClient(
|
|
withInterceptors([credentialsInterceptor]) // Register it here
|
|
)
|
|
]
|
|
};
|
|
|
|
2. Individual Request ConfigurationIf you only want to send cookies to specific endpoints,
|
|
pass the withCredentials option directly inside your service.
|
|
|
|
typescriptimport { HttpClient } from '@angular/common/http';
|
|
import { inject, Injectable } from '@angular/core';
|
|
|
|
@Injectable({ providedIn: 'root' })
|
|
export class ApiService {
|
|
private http = inject(HttpClient);
|
|
|
|
getData() {
|
|
return this.http.get('https://your-api-domain.com', {
|
|
withCredentials: true // Enables cookies for this request only
|
|
});
|
|
}
|
|
} |