0
0
Angularframework~20 mins

Interceptors for authentication tokens in Angular - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Interceptor Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What does this Angular HTTP interceptor do?
Consider this Angular HTTP interceptor code. What is the main effect on outgoing HTTP requests?
Angular
import { Injectable } from '@angular/core';
import { HttpInterceptor, HttpRequest, HttpHandler } from '@angular/common/http';

@Injectable({ providedIn: 'root' })
export class AuthInterceptor implements HttpInterceptor {
  intercept(req: HttpRequest<any>, next: HttpHandler) {
    const token = localStorage.getItem('authToken');
    if (token) {
      const cloned = req.clone({
        headers: req.headers.set('Authorization', `Bearer ${token}`)
      });
      return next.handle(cloned);
    }
    return next.handle(req);
  }
}
AIntercepts HTTP responses to check for token expiration and logs out the user.
BRemoves any Authorization header from outgoing HTTP requests to prevent token leaks.
CAdds an Authorization header with a Bearer token to all outgoing HTTP requests if a token exists in localStorage.
DModifies the request URL to include the token as a query parameter for authentication.
Attempts:
2 left
💡 Hint
Look at how the request is cloned and what header is added before sending.
📝 Syntax
intermediate
2:00remaining
Identify the syntax error in this Angular interceptor snippet
Which option correctly fixes the syntax error in this Angular HTTP interceptor code?
Angular
intercept(req: HttpRequest<any>, next: HttpHandler) {
  const token = localStorage.getItem('authToken');
  if(token) {
    const cloned = req.clone({
      headers: req.headers.set('Authorization', 'Bearer ' + token)
    });
    return next.handle(cloned);
  }
  return next.handle(req);
}
AAdd a missing semicolon after localStorage.getItem('authToken')
BChange req.clone to req.clone() with empty parentheses
CAdd a missing colon after the intercept method name
DAdd a missing comma after 'Authorization' in headers.set
Attempts:
2 left
💡 Hint
Check the line where token is assigned from localStorage.
🔧 Debug
advanced
2:30remaining
Why does this interceptor fail to add the token header?
This Angular interceptor is supposed to add an Authorization header but the header is missing in requests. What is the cause?
Angular
intercept(req: HttpRequest<any>, next: HttpHandler) {
  const token = localStorage.getItem('authToken');
  if (token) {
    req.headers.set('Authorization', `Bearer ${token}`);
  }
  return next.handle(req);
}
AHttpRequest objects are immutable; headers.set returns a new headers object but does not modify the original request.
BlocalStorage.getItem returns null if no token, causing a runtime error.
CThe interceptor must call next.handle() inside the if block only.
DThe Authorization header must be lowercase 'authorization' to be recognized.
Attempts:
2 left
💡 Hint
Think about how Angular HttpRequest objects handle immutability.
state_output
advanced
2:30remaining
What is the output of this interceptor chain?
Given two interceptors registered in this order: AuthInterceptor adds Authorization header if token exists; LoggerInterceptor logs request headers. What will LoggerInterceptor log if localStorage has token 'abc123'?
Angular
AuthInterceptor intercept(req, next) {
  const token = localStorage.getItem('authToken');
  if (token) {
    const cloned = req.clone({ headers: req.headers.set('Authorization', `Bearer ${token}`) });
    return next.handle(cloned);
  }
  return next.handle(req);
}

LoggerInterceptor intercept(req, next) {
  console.log(req.headers.get('Authorization'));
  return next.handle(req);
}
ALoggerInterceptor logs null because headers are not set yet
BLoggerInterceptor logs 'Bearer abc123'
CLoggerInterceptor logs undefined due to missing header
DLoggerInterceptor logs an error because req.headers.get is not a function
Attempts:
2 left
💡 Hint
Remember interceptors run in the order they are registered.
🧠 Conceptual
expert
3:00remaining
Why use an HTTP interceptor for authentication tokens instead of adding headers manually in each service?
Which is the best reason to use an Angular HTTP interceptor to add authentication tokens rather than setting headers manually in every HTTP call?
AInterceptors allow tokens to be encrypted automatically without extra code.
BInterceptors improve HTTP request speed by caching tokens locally in the browser.
CInterceptors prevent the need for Angular services to import HttpClient.
DInterceptors centralize token handling, reduce code duplication, and automatically add tokens to all requests, improving maintainability and security.
Attempts:
2 left
💡 Hint
Think about code reuse and consistency across many HTTP calls.