Adding the token to the HTTP request header

Like we said, we're not done yet; now that we've got the token, we need to find the proper way to pass it along with each HTTP request, otherwise our server-side ASP.NET controllers wouldn't be able to know that we did authenticate ourselves and that we're authorized to access some potentially restricted resources. To put it in other words, we need to tweak our current HTTP service to make it add our JWT token in the header portion of all our HTTP calls.

There are at least three possible ways to obtain that:

  1. Update all of our Angular Components that issue HTTP requests to make them add the JWT token to the request header before each call.
  2. Create a custom service that wraps the Angular HttpClient and make it append the JWT token to the headers before performing the requests.
  3. Implement a HTTP interceptor that will act right before any request will be sent and make it append the JWT token to the headers before releasing it.

The latter option has been made available since Angular 4.3 with the release of the new HttpClient--which we've been proudly using since Chapter 3, Frontend with Angular, --and is probably the most elegant, robust, and effective way to do what we want.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.133.151.220