I need to implement an if statement in the following logic within my HttpInterceptor:
const authToken = this.auth.getAuthorizationToken();
const authReq = req.clone({
headers: req.headers.set('Authorization', `Bearer ${authToken}`)
});
Prior to updating the headers with the authToken, I want to make sure that it actually exists. I believe this may be related to syntax or a different approach to setting the headers property of req.clone().
My attempt is as follows, but I think there might be a more efficient way to achieve this:
let authReq: HttpRequest<any> = req;
if (authToken) {
authReq = req.clone({
headers: req.headers.set('Authorization', `Bearer ${authToken}`)
});
}