Add injectable token provider for authenticated endpoints #248
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi, we have a scenario where some of our endpoints are secured and some are not.
Using the interceptor pattern leaves us unable to determine if we should add the authorize header or not. As a solution I added an injectable angular token provider.
Basically... if there are "authorization" header values AND the auth type is set to bearer in the swagger file it won't include those as params that are needed to be provided by the API consumer, and will instead try to fetch the token using the api token provider as needed. Additionally, we have two different types of auth tokens
authorization
anddevice-authorization
so this enables us to provide the correct token based on what the endpoint defines in the swagger file.With this pattern we only append the auth header if it is required, and the individual places that we call the api service don't need to have or even know about the token.
This could be merged as-is, but I think it'd cause some breaking changes for anyone depending on sending auth tokens as a param. Perhaps i can be a cli flag?
We're using the forked version as-is, but I just wanted to push this change upstream in case you'd like to incorporate it or if anyone else may need similar functionality.
Usage...
add this to your app module
create a provider service like this...