An HTTP interceptor is a service that can inspect and transform HTTP requests or responses globally before they reach the server or the app. It helps add headers, handle errors, or modify requests centrally.
Click to reveal answer
beginner
Why use an interceptor for authentication tokens?
Using an interceptor lets you automatically add authentication tokens to every HTTP request without repeating code in each service. This keeps your code clean and secure.
Click to reveal answer
intermediate
How do you provide an interceptor in Angular?
You provide an interceptor by adding it to the providers array with the token HTTP_INTERCEPTORS and setting multi: true. This tells Angular to use your interceptor alongside others.
Click to reveal answer
intermediate
What method must an Angular interceptor implement?
An Angular interceptor must implement the intercept(req, next) method. It receives the outgoing request and the next handler, and returns an Observable of the HTTP event.
Click to reveal answer
advanced
How can you handle token expiration in an interceptor?
You can check the server response for 401 errors in the interceptor, then trigger a token refresh or redirect to login. This centralizes token expiration handling.
Click to reveal answer
What does an Angular HTTP interceptor primarily do?
ACreate new HTTP requests manually
BHandle UI events
CReplace Angular services
DModify HTTP requests and responses globally
✗ Incorrect
Interceptors modify HTTP requests and responses before they reach the server or the app.
How do you add an interceptor to Angular's HTTP pipeline?
AImport it in the component directly
BAdd it to the providers array with HTTP_INTERCEPTORS and multi: true
CCall it inside each service method
DDeclare it in the module's declarations
✗ Incorrect
Interceptors must be provided with HTTP_INTERCEPTORS token and multi: true in providers.
Which method must be implemented in an Angular HTTP interceptor?
Aintercept(req, next)
BhandleRequest()
CsendRequest()
DprocessHttp()
✗ Incorrect
The intercept method handles the request and forwards it.
What is a common use of interceptors in authentication?
AAdd authentication tokens to HTTP headers
BEncrypt the entire HTTP request body
CCreate new user accounts
DRender login forms
✗ Incorrect
Interceptors add tokens to headers to authenticate requests.
How can an interceptor respond to a 401 Unauthorized error?
AReload the entire app
BIgnore the error silently
CTrigger token refresh or redirect to login
DAutomatically approve the request
✗ Incorrect
Interceptors can catch 401 errors to refresh tokens or redirect users.
Explain how an Angular HTTP interceptor can be used to add authentication tokens to requests.
Think about modifying the request before sending it.
You got /4 concepts.
Describe how you would handle expired tokens using an Angular interceptor.
Focus on response handling inside the interceptor.
You got /4 concepts.
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.