Middleware Authorization
Learn how to add authorization checks using middleware.
We'll cover the following...
Authorization middleware
Adding authorization checks inside all our resolvers will produce clutter, so let’s build middleware to handle this problem fully. As you may recall, we have two choices for how to apply middleware:.We can use the middleware/2 macro to configure individual fields or the middleware/3 callback function to take a pattern-based approach. Different fields have slightly different authorization conditions, so we will use the middleware/2 macro to annotate them individually. Using this middleware should look something like this:
Then, in the resolver, we can just go back to how it was before the authorization check:
Note: We no longer need to check the type of the
current_userin the resolution function or handle the possibility that there is nocurrent_userat all. The middleware will handle all of this for us.
By specifying @behaviour ``Absinthe.Middleware, the compiler will ...