In order to protect an API a function may retrieve a Bearer token from the HTTP request.
As I wanted to to the same for multiple functions I thought I may implement that in an preInvocation hook. However, a hook has no access to the request.
I really wonder what are the use cases for invocation hooks.
I hoped to find something similar to ExpressJS middlewares.
Thanks
You’re correct that Azure Functions currently don’t offer pre-invocation hooks with access to the HTTP request directly. However, there are alternative approaches to achieve token-based authentication across multiple functions:
1. Function-Level Authentication (Simple Cases):
If your authentication logic is relatively simple and doesn’t require complex reuse, you can integrate token retrieval and validation directly within each function:.
This approach keeps the code localized but might not be the most maintainable for complex scenarios.
2. Shared Authentication Module (Recommended for Reusability):
For reusable and more maintainable authentication logic, create a separate module to handle token validation:
authmodule into your function files and leverage itsvalidateTokenmethod.Use Cases for Invocation Hooks (While Not Ideal for Token Retrieval):
While not suitable for request object access, invocation hooks in Azure Functions (Node.js) have other valuable use cases:
If you need more advanced authentication with Azure Functions, explore integrating with Azure Active Directory (AAD) for centralized authorization and management.