Hello, I'd like to obtain the pair of keys but instead of having just username
and password
, I need to have a more complexe structure (because I'm building a mobile app).
To better illustrate, here is a simple unit test:
class UserAuthTest(TestCase):
def test_user_login(self):
user = User.objects.create_user(
phone_number="+1234567890",
password="12345",
)
device = DeviceFactory(user=user)
response = self.client.post(
"/api/v1/token/pair",
{
"user": {"phone_number": "+1234567890", "password": "12345"},
"device": {"vendor_uuid": device.vendor_uuid},
},
content_type="application/json",
)
json = response.json()
self.assertEqual(json, {
"id": "some-id",
"access": "accesstoken",
"is_new_user": False,
"is_new_device": False,
"user": {
"uuid": user.uuid,
},
"device": {
"uuid": device.uuid,
}
})
self.assertEqual(response.status_code, 200)
I override the schema
class UserSchema(Schema):
uuid: UUID
class MyTokenObtainPairOutSchema(Schema):
refresh: str
access: str
user: UserSchema
class TokenSessionInputSchema(TokenObtainPairInputSchema):
def output_schema(self):
out_dict = self.get_response_schema_init_kwargs()
out_dict.update(user=UserSchema.from_orm(self._user))
return MyTokenObtainPairOutSchema(**out_dict)
@classmethod
def validate_values(cls, values: Dict) -> Dict:
user = values["user"]
return {**values, "user": super().validate_values(user)}
However I got the following error:
{'detail': [{'type': 'missing', 'loc': ['body', 'user_token', 'password'], 'msg': 'Field required'}, {'type': 'missing', 'loc': ['body', 'user_token', 'phone_number'], 'msg': 'Field required'}]}
.
In the return of validate_values, he doesn't find the password
and phone_number
field. So when fixing the validate_values
function like following
@classmethod
def validate_values(cls, values: Dict) -> Dict:
user = values["user"]
return super().validate_values(user)
I got another error: pydantic_core._pydantic_core.ValidationError: 1 validation error for NinjaResponseSchema response.phone_number
but I did not specify phone_number
for django-ninja-jwt so I don't know how did he found phone_number
.
Well sorry for this long issue. I tried debugging but I don't know how to configure a custom input (and not only username
and password
) and keep the data inside the classes to have custom behavior (for example creating a device when there is none in the database).
Thanks for your help.