Skip to content

Commit 24186aa

Browse files
authored
Merge pull request #63 from eadwinCode/async_auth_class_doc
Asynchronous Auth Classes Documentation
2 parents afdfcd2 + 361650e commit 24186aa

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

docs/tutorial/authentication.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,5 +48,37 @@ api = NinjaExtraAPI(auth=GlobalAuth())
4848
```
4949
Read more on django-ninja [authentication](https://django-ninja.rest-framework.com/tutorial/authentication/)
5050

51+
## Asynchronous Auth Classes
52+
53+
Ninja Extra added Asynchronous support for all `Auth` base classes provided by Django Ninja in `ninja_extra.security` package.
54+
And it maintained similar interface. It is important to noted that when using these asynchronous auth classes, the endpoint handler
55+
**must** asynchronous functions.
56+
57+
For example, lets re-write the first auth example with `AsyncHttpBearer` class.
58+
59+
```Python
60+
from ninja_extra import api_controller, route
61+
from ninja_extra.security import AsyncHttpBearer
62+
from ninja.constants import NOT_SET
63+
64+
65+
class AuthBearer(AsyncHttpBearer):
66+
async def authenticate(self, request, token):
67+
# await some actions
68+
if token == "supersecret":
69+
return token
70+
71+
72+
@api_controller(tags=['My Operations'], auth=NOT_SET, permissions=[])
73+
class MyController:
74+
@route.get("/bearer", auth=AuthBearer())
75+
async def bearer(self):
76+
return {"token": self.request.auth}
77+
78+
```
79+
In example above, we changed `HttpBearer` to `AsyncHttpBearer` and changed bearer to `async` endpoint.
80+
If `AuthBearer` is to be applied to a `MyController` **auth**, then all route handlers under `MyController` must be asynchronous route handlers.
81+
82+
5183
## **JWT Authentication**
5284
if you want to use JWT authentication. See [ninja-jwt](https://pypi.org/project/django-ninja-jwt/)

0 commit comments

Comments
 (0)