What if you could secure every server call with one simple piece of code?
Why Interceptors for authentication tokens in Angular? - Purpose & Use Cases
Imagine you have to add your login token to every single request manually in your Angular app. You open each service, find every HTTP call, and add the token header by hand.
This manual method is slow and error-prone. You might forget to add the token somewhere, causing requests to fail. It's also hard to update the token logic later because you have to change many places.
Angular interceptors automatically add authentication tokens to all outgoing HTTP requests. You write the logic once, and it runs everywhere, keeping your code clean and consistent.
http.get(url, { headers: { Authorization: 'Bearer ' + token } })intercept(req, next) { const cloned = req.clone({ headers: req.headers.set('Authorization', 'Bearer ' + token) }); return next.handle(cloned); }This lets you secure all your API calls effortlessly and update token handling in one place, improving security and maintainability.
Think of a shopping app where every request to the server needs your login token. Using interceptors, the app automatically adds your token, so you stay logged in without interruptions.
Manually adding tokens is repetitive and risky.
Interceptors automate token addition for all requests.
This improves security, reduces bugs, and simplifies updates.
Practice
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 BQuick Check:
Interceptors add tokens = B [OK]
- Thinking interceptors create requests
- Believing interceptors block requests
- Assuming interceptors change URLs
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.Final Answer:
request.clone({ headers: request.headers.set('Authorization', 'Bearer ' + token) }) -> Option DQuick Check:
Clone request with headers.set() = A [OK]
- Trying to modify headers directly without cloning
- Using non-existent methods like modifyHeaders or updateHeaders
- Not using the clone method
intercept(request: HttpRequest<any>, next: HttpHandler) {
const token = 'abc123';
const cloned = request.clone({
headers: request.headers.set('Authorization', `Bearer ${token}`)
});
return next.handle(cloned);
}Solution
Step 1: Check how the token is added
The code sets the Authorization header to 'Bearer ' plus the token string 'abc123'.Step 2: Understand string interpolation
The template string `Bearer ${token}` becomes 'Bearer abc123'.Final Answer:
Authorization: Bearer abc123 -> Option AQuick Check:
Header value = 'Bearer abc123' [OK]
- Forgetting 'Bearer ' prefix
- Using the literal word 'token' instead of variable
- Assuming header is not added
intercept(req: HttpRequest<any>, next: HttpHandler) {
const token = 'xyz';
req.headers.set('Authorization', 'Bearer ' + token);
return next.handle(req);
}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 AQuick Check:
Immutable headers require cloning = A [OK]
- Trying to set headers directly on request
- Ignoring immutability of HttpRequest
- Not returning next.handle()
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 CQuick Check:
Conditional token addition by URL check = C [OK]
- Adding tokens to all requests blindly
- Modifying requests without cloning
- Trying to add tokens after request completes
Ready to prove it?
Pass the test and this pattern moves unknown
