I work on a single page app authenticating with API by exchanging JWT via Authorization header. The standard way of adding such functionality in AngularJS is to implement an interceptor:

When we do that, every request performed using $http will have added authorization transparently.

That’s a great solution for Ajax, but sometimes we also need to download files. It’s possible, but painful:

For most of the time, I’m ok with such solution, but there is something within me that tries to make it better at an expense of additional complexity. The ultimate goal I have in mind is to make downloading files as simple as:

<a href='/api/file'>Download file</a>

Without any response buffering on a heap.

Here’s the idea how to do that:

we can use auth service for holding JWT and performing logging in and logging out

service worker will intercept all requests to /api and add headers on the fly

Will it be difficult to implement? Let’s find out!