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

Add @smart_token_user decorator to get user object easier #262

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from

Conversation

xros
Copy link

@xros xros commented Jan 19, 2022

I wrote a decorator.

With this,
in our app views.py
we can easily get user object by doing so,@smart_token_user will modify the request handler. We can have a request.user attr only once the token is valid. And all invalid attempts will be thrown out with HTTP 401 Unauthorized response.
Life can be easier with this decorator.

from knox.models import smart_token_user

class CheckUserEmail(generics.RetrieveAPIView):
    @smart_token_user
    def get(self, request):
        return Response({
            "username": request.user.username,
            "email": request.user.email,
            "password": request.user.password,
        }, status=status.HTTP_200_OK)

@giovannicimolin
Copy link
Contributor

@xros Thank you for your contribution! 😁

I'm understanding the purpose of these changes. If you are using Know with Django REST Framework, DRF's internal mechanisms will already handle the authentication step and make the user object available through request.user without any extra code.

Can you explain a bit better the case where you ran into issues, so I can check if your use case is valid?
Let me know if you still have this issue.

Best!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants