|
| 1 | +## Auth Required |
| 2 | + |
| 3 | +You can limit access to a component to users with a specific `auth_attribute` by using this decorator. |
| 4 | + |
| 5 | +By default, this decorator checks if the user is logged in, and his/her account has not been deactivated. |
| 6 | + |
| 7 | +Common uses of this decorator are to hide components from [`AnonymousUser`](https://docs.djangoproject.com/en/dev/ref/contrib/auth/#django.contrib.auth.models.AnonymousUser), or render a component only if the user [`is_staff`](https://docs.djangoproject.com/en/dev/ref/contrib/auth/#django.contrib.auth.models.User.is_staff) or [`is_superuser`](https://docs.djangoproject.com/en/dev/ref/contrib/auth/#django.contrib.auth.models.User.is_superuser). |
| 8 | + |
| 9 | +This decorator can be used with or without parentheses. |
| 10 | + |
| 11 | +```python title="components.py" |
| 12 | +from django_idom.decorators import auth_required |
| 13 | +from django_idom.hooks import use_websocket |
| 14 | +from idom import component, html |
| 15 | + |
| 16 | +@component |
| 17 | +@auth_required |
| 18 | +def my_component(): |
| 19 | + return html.div("I am logged in!") |
| 20 | +``` |
| 21 | + |
| 22 | +??? example "See Interface" |
| 23 | + |
| 24 | + <font size="4">**Parameters**</font> |
| 25 | + |
| 26 | + | Name | Type | Description | Default | |
| 27 | + | --- | --- | --- | --- | |
| 28 | + | auth_attribute | `str` | The value to check within the user object. This is checked in the form of `UserModel.<auth_attribute>`. | `#!python "is_active"` | |
| 29 | + | fallback | `ComponentType`, `VdomDict`, `None` | The `component` or `idom.html` snippet to render if the user is not authenticated. | `None` | |
| 30 | + |
| 31 | + <font size="4">**Returns**</font> |
| 32 | + |
| 33 | + | Type | Description | |
| 34 | + | --- | --- | |
| 35 | + | `Component` | An IDOM component. | |
| 36 | + | `VdomDict` | An `idom.html` snippet. | |
| 37 | + | `None` | No component render. | |
| 38 | + |
| 39 | +??? question "How do I render a different component if authentication fails?" |
| 40 | + |
| 41 | + You can use a component with the `fallback` argument, as seen below. |
| 42 | + |
| 43 | + ```python title="components.py" |
| 44 | + from django_idom.decorators import auth_required |
| 45 | + from django_idom.hooks import use_websocket |
| 46 | + from idom import component, html |
| 47 | + |
| 48 | + @component |
| 49 | + def my_component_fallback(): |
| 50 | + return html.div("I am NOT logged in!") |
| 51 | + |
| 52 | + @component |
| 53 | + @auth_required(fallback=my_component_fallback) |
| 54 | + def my_component(): |
| 55 | + return html.div("I am logged in!") |
| 56 | + ``` |
| 57 | + |
| 58 | +??? question "How do I render a simple `idom.html` snippet if authentication fails?" |
| 59 | + |
| 60 | + You can use a `idom.html` snippet with the `fallback` argument, as seen below. |
| 61 | + |
| 62 | + ```python title="components.py" |
| 63 | + from django_idom.decorators import auth_required |
| 64 | + from django_idom.hooks import use_websocket |
| 65 | + from idom import component, html |
| 66 | + |
| 67 | + @component |
| 68 | + @auth_required(fallback=html.div("I am NOT logged in!")) |
| 69 | + def my_component(): |
| 70 | + return html.div("I am logged in!") |
| 71 | + ``` |
| 72 | + |
| 73 | +??? question "How can I check if a user `is_staff`?" |
| 74 | + |
| 75 | + You can set the `auth_attribute` to `is_staff`, as seen blow. |
| 76 | + |
| 77 | + ```python title="components.py" |
| 78 | + from django_idom.decorators import auth_required |
| 79 | + from django_idom.hooks import use_websocket |
| 80 | + from idom import component, html |
| 81 | + |
| 82 | + |
| 83 | + @component |
| 84 | + @auth_required(auth_attribute="is_staff") |
| 85 | + def my_component(): |
| 86 | + return html.div("I am logged in!") |
| 87 | + ``` |
| 88 | + |
| 89 | +??? question "How can I check for a custom attribute?" |
| 90 | + |
| 91 | + You will need to be using a [custom user model](https://docs.djangoproject.com/en/dev/topics/auth/customizing/#specifying-a-custom-user-model) within your Django instance. |
| 92 | + |
| 93 | + For example, if your user model has the field `is_really_cool` ... |
| 94 | + |
| 95 | + ```python |
| 96 | + from django.contrib.auth.models import AbstractBaseUser |
| 97 | + |
| 98 | + class CustomUserModel(AbstractBaseUser): |
| 99 | + @property |
| 100 | + def is_really_cool(self): |
| 101 | + return True |
| 102 | + ``` |
| 103 | + |
| 104 | + ... then you would do the following within your decorator: |
| 105 | + |
| 106 | + ```python title="components.py" |
| 107 | + from django_idom.decorators import auth_required |
| 108 | + from django_idom.hooks import use_websocket |
| 109 | + from idom import component, html |
| 110 | + |
| 111 | + @component |
| 112 | + @auth_required(auth_attribute="is_really_cool") |
| 113 | + def my_component(): |
| 114 | + return html.div("I am logged in!") |
| 115 | + ``` |
0 commit comments