|
| 1 | +# Async auth mixins |
| 2 | + |
| 3 | +django has a set of mixins that help with limiting access and enforcing authentication and permissions, |
| 4 | +but these mixins do not work with async views. |
| 5 | + |
| 6 | +to help with that an async version of the mixins are included in the `acontrib.auth.mixins` module. |
| 7 | + |
| 8 | +**note**: these mixins only work with `AsyncView` or classes that inherit from it, or implement the same logic. |
| 9 | + |
| 10 | +## AsyncAccessMixin |
| 11 | +[django.contrib.auth.mixins.AccessMixin](https://docs.djangoproject.com/en/5.1/topics/auth/default/#django.contrib.auth.mixins.AccessMixin) handles the behaviour of a view when access should be denied, |
| 12 | +the `AsyncAccessMixin` class works similarly to django's version, except that the `handle_no_permission()` method is async. |
| 13 | + |
| 14 | +## AsyncLoginRequiredMixin |
| 15 | +works similar to [LoginRequiredMixin](https://docs.djangoproject.com/en/5.1/topics/auth/default/#the-loginrequiredmixin-mixin) but inherits from `AsyncAccessMixin`, |
| 16 | +also the `dispatch()` method is async. |
| 17 | + |
| 18 | +```python |
| 19 | +from django_async_extensions.acontrib.auth.mixins import AsyncLoginRequiredMixin |
| 20 | + |
| 21 | + |
| 22 | +class MyView(AsyncLoginRequiredMixin, AsyncView): |
| 23 | + login_url = "/login/" |
| 24 | + redirect_field_name = "redirect_to" |
| 25 | +``` |
| 26 | + |
| 27 | +## AsyncPermissionRequiredMixin |
| 28 | +works similar to [PermissionRequiredMixin](https://docs.djangoproject.com/en/5.1/topics/auth/default/#the-permissionrequiredmixin-mixin) |
| 29 | +with a few differences: |
| 30 | + |
| 31 | +1. `AsyncPermissionRequiredMixin` inherits from `AsyncAccessMixin`. |
| 32 | +2. `has_permission()` method is async. |
| 33 | +3. `dispatch()` method is async. |
| 34 | + |
| 35 | +## AsyncUserPassesTestMixin |
| 36 | +works similar to [UserPassesTestMixin](https://docs.djangoproject.com/en/5.1/topics/auth/default/#django.contrib.auth.mixins.UserPassesTestMixin) |
| 37 | +with a few differences: |
| 38 | + |
| 39 | +1. `AsyncUserPassesTestMixin` inherits from `AsyncAccessMixin`. |
| 40 | +2. `test_func()` method is async. |
| 41 | +3. `dispatch()` method is async. |
0 commit comments