📖 Scenario: You are building a simple Angular app that needs to send an authentication token with every HTTP request to a server. This token proves the user is logged in.To avoid adding the token manually in every request, you will create an HTTP interceptor. This interceptor will automatically add the token to the request headers.
🎯 Goal: Create an Angular HTTP interceptor that adds a fixed authentication token to the Authorization header of all outgoing HTTP requests.
📋 What You'll Learn
Create a constant token string called authToken with value 'Bearer abc123token'.
Create an Angular HTTP interceptor class called AuthInterceptor.
In the interceptor, add the Authorization header with the authToken to every outgoing HTTP request.
Register the interceptor as a provider in the Angular dependency injection system.
💡 Why This Matters
🌍 Real World
Most web apps need to send authentication tokens with API requests to prove the user is logged in. Interceptors automate this process.
💼 Career
Understanding Angular interceptors is essential for frontend developers working with secure APIs and authentication.
Progress0 / 4 steps
1
DATA SETUP: Create the authentication token constant
Create a constant string called authToken and set it to 'Bearer abc123token'.
Angular
Hint
This token will be added to HTTP request headers later.
2
CONFIGURATION: Create the AuthInterceptor class skeleton
Create an Angular HTTP interceptor class called AuthInterceptor that implements HttpInterceptor. Import HttpInterceptor and HttpRequest from @angular/common/http. Add the method intercept(req: HttpRequest, next: HttpHandler) returning next.handle(req).
Angular
Hint
Start with a basic interceptor that just passes requests through.
3
CORE LOGIC: Add the Authorization header with the token
Inside the intercept method, clone the incoming req and add a new header Authorization with the value of authToken. Then pass the cloned request to next.handle().
Angular
Hint
Use req.clone() to add headers without changing the original request.
4
COMPLETION: Register the interceptor provider
Add a provider object to register AuthInterceptor as an HTTP interceptor. Use provide: HTTP_INTERCEPTORS, useClass: AuthInterceptor, and multi: true. Import HTTP_INTERCEPTORS from @angular/common/http.
Angular
Hint
This provider will be added to your Angular module's providers array.
Practice
(1/5)
1. What is the main purpose of an HTTP interceptor in Angular when dealing with authentication tokens?
easy
A. To create new HTTP requests manually
B. To automatically add authentication tokens to outgoing HTTP requests
C. To block all HTTP requests from the app
D. To change the URL of HTTP requests
Solution
Step 1: Understand the role of interceptors
Interceptors are used to modify HTTP requests or responses globally in Angular.
Step 2: Identify the use of tokens in interceptors
They commonly add authentication tokens to requests to keep the app secure without repeating code.
Final Answer:
To automatically add authentication tokens to outgoing HTTP requests -> Option B
Quick Check:
Interceptors add tokens = B [OK]
Hint: Interceptors modify requests automatically, especially for tokens [OK]
Common Mistakes:
Thinking interceptors create requests
Believing interceptors block requests
Assuming interceptors change URLs
2. Which of the following is the correct way to clone an HTTP request before adding an authentication token in an Angular interceptor?
easy
A. request.updateHeaders({ Authorization: 'Bearer ' + token })
B. request.headers.set('Authorization', 'Bearer ' + token)
C. request.modifyHeaders('Authorization', 'Bearer ' + token)
D. request.clone({ headers: request.headers.set('Authorization', 'Bearer ' + token) })
Solution
Step 1: Recall Angular HttpRequest immutability
HttpRequest objects are immutable, so you must clone them to modify headers.
Step 2: Use the clone method with headers.set()
The correct syntax is to call clone() and set the new header inside it.
A. Headers are immutable; must clone request before setting headers
B. Token variable is not defined
C. next.handle() is missing a return statement
D. Authorization header should be lowercase
Solution
Step 1: Understand HttpRequest immutability
HttpRequest headers cannot be changed directly; they are immutable.
Step 2: Correct way is to clone the request with new headers
You must clone the request and set headers on the clone before passing it on.
Final Answer:
Headers are immutable; must clone request before setting headers -> Option A
Quick Check:
Immutable headers require cloning = A [OK]
Hint: Headers are immutable; clone request before modifying [OK]
Common Mistakes:
Trying to set headers directly on request
Ignoring immutability of HttpRequest
Not returning next.handle()
5. You want to create an Angular interceptor that adds an authentication token only to requests targeting your API domain 'api.example.com'. Which approach correctly implements this conditional logic?
hard
A. Modify the token inside next.handle() without cloning
B. Add the token to all requests unconditionally
C. Check if request.url.includes('api.example.com') before cloning and adding the token
D. Use a global variable to store the token and add it after the request completes
Solution
Step 1: Identify the need for conditional token addition
Only requests to 'api.example.com' should have the token added to avoid leaking tokens to other domains.
Step 2: Use URL check before cloning and modifying request
Check request.url.includes('api.example.com') and then clone the request with the token header.
Step 3: Avoid modifying requests without cloning or adding tokens globally
Modifying without cloning or adding tokens to all requests is insecure or incorrect.
Final Answer:
Check if request.url.includes('api.example.com') before cloning and adding the token -> Option C
Quick Check:
Conditional token addition by URL check = C [OK]
Hint: Add tokens only after checking request URL [OK]
Common Mistakes:
Adding tokens to all requests blindly
Modifying requests without cloning
Trying to add tokens after request completes
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.