0
0
Angularframework~5 mins

Interceptors for authentication tokens in Angular - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
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?
ACreate new HTTP requests manually
BHandle UI events
CReplace Angular services
DModify HTTP requests and responses globally
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
Which method must be implemented in an Angular HTTP interceptor?
Aintercept(req, next)
BhandleRequest()
CsendRequest()
DprocessHttp()
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
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
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.