Recall & Review
beginner
What is an HTTP interceptor in Angular?
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?
✗ 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?
✗ Incorrect
Interceptors must be provided with HTTP_INTERCEPTORS token and multi: true in providers.
Which method must be implemented in an Angular HTTP interceptor?
✗ Incorrect
The intercept method handles the request and forwards it.
What is a common use of interceptors in authentication?
✗ Incorrect
Interceptors add tokens to headers to authenticate requests.
How can an interceptor respond to a 401 Unauthorized error?
✗ 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.