0

I tried some ways but I couldn't solve it. If someone can give me an idea. I'm using environments to standardize the routes of my application.

My Interceptor:

import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent} from '@angular/common/http';
import { Observable } from 'rxjs';
import { Injectable } from '@angular/core';
    
import { TokenService } from '../token/token.service';
    
@Injectable()
export class RequestInterceptor implements HttpInterceptor {
    
   constructor(private tokenService: TokenService){}
    
   intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {
       if(this.tokenService.hasToken()){
            const token = this.tokenService.getToken();
            req = req.clone({
                    setHeaders: {
                        'Authorization': 'bearer ' + token
                    }
            });
       }
       return next.handle(req);
    }
}

Module core:

import { CommonModule } from '@angular/common';
import { HTTP_INTERCEPTORS } from '@angular/common/http';
import { NgModule } from '@angular/core';
import { RouterModule } from '@angular/router';
import { ContainerFluidModule } from '../shared/components/container-fluid/container-fluid.module';
import { RequestInterceptor } from './auth/request-interceptor';
import { MenuComponent } from './menu/menu.component';
    
    
@NgModule({
        declarations: [
            MenuComponent,
        ],
        exports: [
            MenuComponent,
        ],
        imports : [
            CommonModule,
            RouterModule,
            ContainerFluidModule
        ],
        providers: [
            {
                provide: HTTP_INTERCEPTORS,
                useClass: RequestInterceptor, 
                multi: true
            }
        ]
})
export class CoreModule {} 

Edit post, to exemplify how the application is working, I will post how I managed to solve it and get the application ready in case I need to add a new API: D

2
  • Hi. welcome to stack overflow. Could you explain clearly what the question is Commented Apr 9, 2021 at 5:29
  • Hi. thanks, i will edit the question and add how i managed to solve it, i spent the night researching Commented Apr 9, 2021 at 11:44

2 Answers 2

0

I know that it might be a shot in the dark but I found a useful technique for dealing with multiple APIs when using interceptors:

Let us know if it helps ;)

Sign up to request clarification or add additional context in comments.

Comments

0

I solved my problem using this link and documentation Angular

enter link description here

My new code looks like this.

import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from '@angular/common/http';
import { Observable } from 'rxjs';
import { Injectable } from '@angular/core';

import { TokenService } from '../token/token.service';
import { environment } from 'src/environments/environment';

const API_URL = environment.API_URL;

@Injectable()
export class RequestInterceptor implements HttpInterceptor {

    constructor(private tokenService: TokenService) { }

    intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> {

        if (this.tokenService.hasToken()) {
            if (!req.url.indexOf(API_URL)) {
                const token = this.tokenService.getToken();
                req = req.clone({
                    setHeaders: {
                        'Authorization': 'bearer ' + token
                    }
                });
            }
            return next.handle(req);
        }
    }
}

The only thing I changed was to check the url using the indexOf () method and if it is the standard URL of my API I send the header if not only return the request.

The tip is already in case you need to add more APIS to the applications.

Thank you all so much for your help :D

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.