@@ -48,5 +48,37 @@ api = NinjaExtraAPI(auth=GlobalAuth())
4848```
4949Read 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**
5284if you want to use JWT authentication. See [ ninja-jwt] ( https://pypi.org/project/django-ninja-jwt/ )
0 commit comments