Yes. I have extracted from project you sent.
I have checked two files and all changes can be seen.

On Sun, Dec 1, 2019 at 4:57 AM Oleg Nykolyn <[email protected]> wrote:

> Hi,
>
> I get following output:
>
> [30/Nov/2019 20:53:35] *"POST /api/test/ HTTP/1.1" 405 41*
>
> admin
>
> None
>
> ***************
>
> [30/Nov/2019 20:54:34] "GET /api/test/ HTTP/1.1" 200 18
>
> Have you extracted code from archive attached to previous message ? It
> contains changes in middleware.py and settings.py - extra middleware is
> added there:
>
> ```
>
> MIDDLEWARE = [
>
>     'django.middleware.security.SecurityMiddleware',
>
>     'django.contrib.sessions.middleware.SessionMiddleware',
>
>     'django.middleware.common.CommonMiddleware',
>
>     'django.middleware.csrf.CsrfViewMiddleware',
>
>     'django.contrib.auth.middleware.AuthenticationMiddleware',
>
>     'django.contrib.messages.middleware.MessageMiddleware',
>
>     'django.middleware.clickjacking.XFrameOptionsMiddleware',
>
>     'middleware.middleware.AuthenticationTokenMiddleware',
>
>     'middleware.middleware.PermissionMiddleware'
>
> ]
>
> ```
>
>
> On Sat, Nov 30, 2019 at 10:50 PM Yery cs <[email protected]> wrote:
>
>>
>>
>> On Sun, Dec 1, 2019 at 4:47 AM Yery cs <[email protected]> wrote:
>>
>>> Hello. thanks for your help.
>>> Unfortunately, I still get AnonymousUser.
>>> Can you check once more again?
>>>
>>> On Sun, Dec 1, 2019 at 4:40 AM Oleg Nykolyn <[email protected]> wrote:
>>>
>>>> Hi,
>>>>
>>>> Here is custom middleware, which works for this case, also
>>>> attached project with applied changes -  it now show "admin" instead of
>>>> AnonymousUser.
>>>> Reason why DRF doesn't show user in middleware - DRF does not provide
>>>> it's own middleware for this and sets user only when it processes views. So
>>>> any other middleware won't be able to see requests.user, unless Django's
>>>> middleware will set it (not DRF!), but this means cookie/session auth.
>>>> I've spent a few day on same issue once, it's unfortunate that DRF
>>>> doesn't provide built-in middleware for this, but workaround is quite
>>>> simple and reliable.
>>>> ```
>>>>
>>>> from rest_framework_jwt.authentication import JSONWebTokenAuthentication
>>>>
>>>> from django.utils.functional import SimpleLazyObject
>>>>
>>>>
>>>>
>>>> # Workaround simialr to
>>>> https://github.com/GetBlimp/django-rest-framework-jwt/issues/45 # noqa
>>>>
>>>> class AuthenticationTokenMiddleware:
>>>>
>>>>     """Authentication middleware which return user from token."""
>>>>
>>>>
>>>>     def __init__(self, get_response):
>>>>
>>>>         """Initializer."""
>>>>
>>>>         self.get_response = get_response
>>>>
>>>>
>>>>     def __call__(self, request):
>>>>
>>>>         """Response."""
>>>>
>>>>         user = request.user
>>>>
>>>>         request.user = SimpleLazyObject(lambda:
>>>> self.get_token_user(request,
>>>>
>>>>
>>>> user))
>>>>
>>>>         return self.get_response(request)
>>>>
>>>>
>>>>     def get_token_user(self, request, user):
>>>>
>>>>         """Return user from DRF token."""
>>>>
>>>>         try:
>>>>
>>>>             authenticator = JSONWebTokenAuthentication()
>>>>
>>>>             return authenticator.authenticate(request)[0]
>>>>
>>>>         except Exception:
>>>>
>>>>             return user
>>>> ```
>>>>
>>>> On Sat, Nov 30, 2019 at 10:34 PM Wanderley S <[email protected]> wrote:
>>>>
>>>>> Nothing to be sorry about.
>>>>> I'll take a look at your code later, since I'm on mobile now.
>>>>>
>>>>> Can you show me the code where you make the request?
>>>>>
>>>>> You're using curl or some javascript client?
>>>>>
>>>>>
>>>>> Em sáb, 30 de nov de 2019 17:10, Yery cs <[email protected]>
>>>>> escreveu:
>>>>>
>>>>>> Hello, thanks for you reply.
>>>>>> Sorry for my poor post. I understand concepts of Django
>>>>>> authentication.
>>>>>> I am using DRF jwt.
>>>>>> I have added my custom middleware after Authentication middleware in
>>>>>> settings file.
>>>>>> And I am printing request.user in console like this `print
>>>>>> (request.user)` in my custom middleware.
>>>>>> But I get still AnonymousUser.
>>>>>> Can you tell me why it is, and how can I fix it?
>>>>>> Thank you.
>>>>>>
>>>>>>
>>>>>>>
>>>>>>> --
>>>>>> You received this message because you are subscribed to the Google
>>>>>> Groups "Django REST framework" group.
>>>>>> To unsubscribe from this group and stop receiving emails from it,
>>>>>> send an email to [email protected].
>>>>>> To view this discussion on the web visit
>>>>>> https://groups.google.com/d/msgid/django-rest-framework/CALCmTPEd-rLHmS%2BazDR2Zj-NR7ZtkbVhEEQcvdER%3Dbq0%3DU-5vA%40mail.gmail.com
>>>>>> <https://groups.google.com/d/msgid/django-rest-framework/CALCmTPEd-rLHmS%2BazDR2Zj-NR7ZtkbVhEEQcvdER%3Dbq0%3DU-5vA%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>>>>> .
>>>>>>
>>>>> --
>>>>> You received this message because you are subscribed to the Google
>>>>> Groups "Django REST framework" group.
>>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>>> an email to [email protected].
>>>>> To view this discussion on the web visit
>>>>> https://groups.google.com/d/msgid/django-rest-framework/CA%2Ba7aJ3efg3%2BBzgcxeTSCZwP6pw%3Dk3NoecUsJ0QNN_7nREErSQ%40mail.gmail.com
>>>>> <https://groups.google.com/d/msgid/django-rest-framework/CA%2Ba7aJ3efg3%2BBzgcxeTSCZwP6pw%3Dk3NoecUsJ0QNN_7nREErSQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>>>> .
>>>>>
>>>> --
>>>> You received this message because you are subscribed to the Google
>>>> Groups "Django REST framework" group.
>>>> To unsubscribe from this group and stop receiving emails from it, send
>>>> an email to [email protected].
>>>> To view this discussion on the web visit
>>>> https://groups.google.com/d/msgid/django-rest-framework/CALqbk1Rx7pQ35-2BZ3rS0tFMwzYLe%2B5wHNS8Z%3DgSjxD6ttc%2Brw%40mail.gmail.com
>>>> <https://groups.google.com/d/msgid/django-rest-framework/CALqbk1Rx7pQ35-2BZ3rS0tFMwzYLe%2B5wHNS8Z%3DgSjxD6ttc%2Brw%40mail.gmail.com?utm_medium=email&utm_source=footer>
>>>> .
>>>>
>>> --
>> You received this message because you are subscribed to the Google Groups
>> "Django REST framework" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> To view this discussion on the web visit
>> https://groups.google.com/d/msgid/django-rest-framework/CALCmTPEQCBjUPv_ZF2%2BSn6Xw-f%2BvjvbrFF22XwK0L0ktR7Zd8Q%40mail.gmail.com
>> <https://groups.google.com/d/msgid/django-rest-framework/CALCmTPEQCBjUPv_ZF2%2BSn6Xw-f%2BvjvbrFF22XwK0L0ktR7Zd8Q%40mail.gmail.com?utm_medium=email&utm_source=footer>
>> .
>>
> --
> You received this message because you are subscribed to the Google Groups
> "Django REST framework" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> To view this discussion on the web visit
> https://groups.google.com/d/msgid/django-rest-framework/CALqbk1QxctvtFSpcsAtczng-H2Xjc9DCBzXC2jNSnycmHTHqpQ%40mail.gmail.com
> <https://groups.google.com/d/msgid/django-rest-framework/CALqbk1QxctvtFSpcsAtczng-H2Xjc9DCBzXC2jNSnycmHTHqpQ%40mail.gmail.com?utm_medium=email&utm_source=footer>
> .
>

-- 
You received this message because you are subscribed to the Google Groups 
"Django REST framework" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-rest-framework/CALCmTPF03Ks5bKeZHzbQ7L6XbssdjmvEhtrCPdnru8Z%3DqV8NJA%40mail.gmail.com.

Reply via email to