1. Anuncie Aqui ! Entre em contato fdantas@4each.com.br

Angular Interceptor Implementation

Discussão em 'Angular' iniciado por Gabro, Novembro 3, 2024 às 10:22.

  1. Gabro

    Gabro Guest

    so I am trying to implement an interceptor for my little app which is standalone: true, and all I want this interceptor to do is check that if the response from the server comes back with error 401 to log user so for that I wrote code with some console.logs to track it
    auth.interceptor.ts

    import { inject, Injectable } from '@angular/core';
    import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest, HttpResponse, HttpErrorResponse } from '@angular/common/http';
    import { Observable, catchError, throwError } from 'rxjs';
    import { Router } from '@angular/router';
    import { AuthService } from '../shared/services/auth.service';

    @Injectable({
    providedIn: 'root',
    })
    export class AuthInterceptor implements HttpInterceptor {
    private authService = inject(AuthService)
    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
    console.log('Interceptor called', req);
    return next.handle(req).pipe(
    catchError((error: HttpErrorResponse) => {
    if (error.status === 401) {
    this.authService.logout();
    }
    return throwError(error);
    })
    );
    }
    }


    my app.config.server.ts looks like that

    import { mergeApplicationConfig, ApplicationConfig } from '@angular/core';
    import { provideServerRendering } from '@angular/platform-server';
    import { appConfig } from './app.config';

    const serverConfig: ApplicationConfig = {
    providers: [
    provideServerRendering()
    ]
    };

    export const config = mergeApplicationConfig(appConfig, serverConfig);


    app.config.ts

    import { ApplicationConfig } from '@angular/core';
    import { provideRouter } from '@angular/router';
    import { routes } from './app-routing.module';
    import { HTTP_INTERCEPTORS, provideHttpClient, withFetch } from '@angular/common/http';
    import { provideAnimations } from '@angular/platform-browser/animations';
    import { AuthInterceptor } from './interceptor/auth.interceptor';
    export const appConfig: ApplicationConfig = {
    providers: [
    provideRouter(routes),
    provideAnimations(),
    provideHttpClient(withFetch()),
    { provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, multi: true}
    ]
    };


    main.server.ts

    import { bootstrapApplication } from '@angular/platform-browser';
    import { AppComponent } from './app/app.component';
    import { config } from './app/app.config.server';

    const bootstrap = () => bootstrapApplication(AppComponent, config);

    export default bootstrap;


    main.ts

    import { bootstrapApplication } from '@angular/platform-browser';
    import { appConfig } from './app/app.config';
    import { AppComponent } from './app/app.component';

    bootstrapApplication(AppComponent, appConfig).catch(err => console.error(err));


    but problem is that I can't even see this console.logs which I have in interceptor like interception is not even being called

    I tried code above but its not working. (standalone: true)

    Continue reading...

Compartilhe esta Página