1-
21If you wish to customize the claims contained in web tokens which are
32generated by the ` NinjaJWTDefaultController ` and ` NinjaJWTSlidingController `
43views, create a subclass for the desired controller as well as a subclass for
54its corresponding serializer. Here\' s an example :
65
76!!! info
8- if you are interested in Asynchronous version of the class, use ` AsyncNinjaJWTDefaultController ` and ` AsyncNinjaJWTSlidingController ` .
9- Also note, it's only available for Django versions that supports asynchronous actions.
7+ if you are interested in Asynchronous version of the class, use ` AsyncNinjaJWTDefaultController ` and ` AsyncNinjaJWTSlidingController ` .
8+ Also note, it's only available for Django versions that supports asynchronous actions.
109
1110``` python
1211from ninja_jwt.schema import TokenObtainPairInputSchema
@@ -18,8 +17,8 @@ from ninja import Schema
1817class UserSchema (Schema ):
1918 first_name: str
2019 email: str
21-
22-
20+
21+
2322class MyTokenObtainPairOutSchema (Schema ):
2423 refresh: str
2524 access: str
@@ -40,34 +39,34 @@ class MyTokenObtainPairController(TokenObtainPairController):
4039 )
4140 def obtain_token (self , user_token : MyTokenObtainPairSchema):
4241 return user_token.output_schema()
43-
42+
4443```
4544
4645As with the standard controller, you\' ll also need to include register the controller as shown in ` getting_started `
4746
4847#### Use Django Ninja Router
48+
4949If you interested in using functions rather than classes, then you are also covered.
5050Here is an example
5151
5252``` python
53- from ninja import router
54-
55- router = router(' /token' )
56-
57- @router.post (
58- " /pair" , response = MyTokenObtainPairOutSchema, url_name = " token_obtain_pair"
59- )
60- def obtain_token (self , user_token : MyTokenObtainPairSchema):
61- return user_token.output_schema()
53+ from ninja_jwt.routers.blacklist import blacklist_router
54+ from ninja_jwt.routers.obtain import obtain_pair_router, sliding_router
55+ from ninja_jwt.routers.verify import verify_router
6256```
6357
6458Register the ` router ` to the django-ninja ` api ` like so:
59+
6560``` python
6661from ninja import NinjaAPI
6762
6863api = NinjaAPI()
69- api.add_router(' ' , tags = [' Auth' ], router = router)
64+ api.add_router(' /token' , tags = [' Auth' ], router = obtain_pair_router)
65+ ...
7066```
67+
68+ If you are interested in customize the token claims, you can do so by creating a subclass of ` TokenObtainPairInputSchema ` and ` TokenObtainPairController ` . See [ Controller Schema Swapping] ( #controller-schema-swapping )
69+
7170Also, its important to note that ` NinjaExtra ` registers a handler for ` APIException ` class which is not available in ` NinjaAPI ` instance.
7271To fix that, you need the extra code below:
7372
@@ -95,8 +94,6 @@ def api_exception_handler(request, exc):
9594api.exception_handler(exceptions.APIException)(api_exception_handler)
9695```
9796
98-
99-
10097### Controller Schema Swapping
10198
10299You can now swap controller schema in ` NINJA_JWT ` settings without having to inherit or override Ninja JWT controller function.
@@ -128,7 +125,7 @@ class MyTokenObtainPairInputSchema(TokenObtainInputSchemaBase):
128125 @ classmethod
129126 def get_response_schema (cls ) -> Type[Schema]:
130127 return MyTokenObtainPairOutSchema
131-
128+
132129 @ classmethod
133130 def get_token (cls , user ) -> Dict:
134131 values = {}
@@ -139,7 +136,7 @@ class MyTokenObtainPairInputSchema(TokenObtainInputSchemaBase):
139136 return values
140137```
141138
142- In the ` MyTokenObtainPairInputSchema ` we override ` get_token ` to define our token and some data needed for our output schema.
139+ In the ` MyTokenObtainPairInputSchema ` we override ` get_token ` to define our token and some data needed for our output schema.
143140We also override ` get_response_schema ` to define our output schema ` MyTokenObtainPairOutSchema ` .
144141
145142Next, we apply the ` MyTokenObtainPairInputSchema ` schema to controller. This is simply done in ` NINJA_JWT ` settings.
@@ -151,11 +148,10 @@ NINJA_JWT = {
151148 ' TOKEN_OBTAIN_PAIR_INPUT_SCHEMA' : ' project.schema.MyTokenObtainPairInputSchema' ,
152149}
153150```
151+
154152Other swappable schemas can be found in [ settings] ( ../settings )
155153
156154![ token_customization_git] ( ./img/token_customize.gif )
157155
158156!!! Note
159- ` Controller Schema Swapping ` is only available from ** v5.2.4**
160-
161-
157+ ` Controller Schema Swapping ` is only available from ** v5.2.4**
0 commit comments