Complete the code to import the HTTP_INTERCEPTORS token.
import { HTTP_INTERCEPTORS } from '@angular/common/http'; @NgModule({ providers: [ { provide: [1], useClass: AuthInterceptor, multi: true } ] }) export class AppModule {}
The HTTP_INTERCEPTORS token is used to register HTTP interceptors in Angular.
Complete the code to implement the intercept method signature.
intercept(req: HttpRequest<any>, [1]: HttpHandler): Observable<HttpEvent<any>> {
// interceptor logic
}The second parameter of the intercept method is conventionally named next and is of type HttpHandler.
Fix the error in cloning the request with the Authorization header.
const authReq = req.[1]({ headers: req.headers.set('Authorization', `Bearer ${token}`) });
The HttpRequest object is immutable, so you must use clone() to modify it.
Fill both blanks to forward the cloned request.
return [1].[2](authReq);
Use next.handle(authReq) to forward the modified request to the next interceptor or backend.
Fill all three blanks to create and provide the interceptor class.
import { Injectable } from '@angular/core'; import { HttpInterceptor, HttpRequest, HttpHandler, HttpEvent } from '@angular/common/http'; import { Observable } from 'rxjs'; @Injectable() export class [1] implements [2] { intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { const token = 'my-token'; const authReq = req.clone({ headers: req.headers.set('Authorization', `Bearer ${token}`) }); return next.[3](authReq); } }
The class name is AuthInterceptor, it implements HttpInterceptor, and uses next.handle() to forward the request.