This module does not provide an authentication strategy, but it provides a very fast utility to handle authentication (also multiple strategies) in your routes, without adding overhead.
Check out the complete example here.
npm i fastify-auth --save
As said above,
fastify-auth does not provide an authentication strategy, so you must provide it (or them) by yourself, with a decorator or another plugin.
In the following example you will find a very simple implementation that should help you understand how use this module.
fastify-auth will run all your authentication methods if at least one succeed it will continue your request, otherwise it will return an error to the client. It will automatically stop as soon as a authentication succeed.
You can use this plugin on route level, as in the above example or on hook level, by using the
The difference between the two approaches is that if you use the
beforeHandler the authentication will run just for the selected route, while if you use the
preHandler hook the authentication will run for all the routes declared inside the current plugin (and its sons).
This project is kindly sponsored by:
Licensed under MIT.