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/CALCmTPHFZuERDqhEoRiiuTyFTDpVtBNK7DU_2%3DLyxC08SODsfw%40mail.gmail.com.
