Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add HTTP Interceptor to standAlone component

I'm trying to add an interceptor to standalone component by adding the interceptor to the providers array in the component itself ( { provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true } ), but it's not working...

here is a link to code

thanks :)

like image 357
Eli Porush Avatar asked Nov 30 '25 10:11

Eli Porush


1 Answers

As of version 15, Angular will be able to use provideHttpClient during bootstrapping of our application and functional interceptors:

bootstrapApplication(AppComponent, {
providers: [
    provideHttpClient(
        withInterceptors([authInterceptor]),
    ),
]

Funtional interceptor:

import { HttpInterceptorFn } from "@angular/common/http";

export const authInterceptor: HttpInterceptorFn = (req, next) => {

    req = req.clone({
        headers
    }

return next(req)
}

We need to add the withInterceptorsFromDi option to provideHttpClient if we can use a class-based interceptor:

bootstrapApplication(AppComponent, {
providers: [
    provideHttpClient(
        withInterceptorsFromDi(),
    ),
    {
        provide: HTTP_INTERCEPTORS,
        useClass: AuthInterceptor,
        multi: true,
    },
 ] });
like image 67
rzyman Avatar answered Dec 07 '25 21:12

rzyman