Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

use USERNAME_FIELD instead username in generate token for user in token authentication #8839

Open
mojtabaakbari221b opened this issue Jan 11, 2023 · 4 comments
Labels

Comments

@mojtabaakbari221b
Copy link

Imagine a situation where someone has chosen email as the USERNAME_FIELD field, with the current logic, it is only possible to create a token by sending the username, but if it is possible to create a token for the USERNAME_FIELD field, it is possible to create a token for any other USERNAME_FIELD field such as mobile number or email.

This idea is taken from the djoser package and has an interesting implementation. Also I can add this feature to drf. Is it worth the time to merge with the package?

@auvipy
Copy link
Member

auvipy commented Jan 11, 2023

can you share the code snippet please?

@mojtabaakbari221b
Copy link
Author

file

In the AuthTokenSerializer class we have:

username = serializers.CharField(
         label=_("Username"),
         write_only=True
     )

If you can write as below, you can have the same capabilities as I said:

class AuthTokenSerializer(serializers.Serializer):
    ...
    def __init__(self, *args, **kwargs):
        self.fields[User.USERNAME_FIELD] = serializers.CharField()

Of course, some other changes are needed elsewhere, but that's the general idea. I don't know if I was able to convey my meaning or not.

@kevin-brown
Copy link
Member

I think this is a near duplicate of #4193.

My understanding was that you could pass whatever the USERNAME_FIELD value is as username within the view and it would map appropriately because of how the Django authenticate method works.

@stale
Copy link

stale bot commented Mar 18, 2023

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@stale stale bot added the stale label Mar 18, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants