I am suck with Django Rest framework issue.
I am trying to use `request.user` in Django Rest Framework middleware.
It returns AnonymousUser and I failed.
I have searched and searched but there is no answer.
I wonder if this is Django Rest framework issue.
Who faced this issue? And did you solve?
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 django-rest-fram...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/4c08081d-0706-4b70-b252-63f8ca8f7d17%40googlegroups.com.
--
Hello, Thanks for your reply.
--
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 django-rest-fram...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/db4aad44-1b01-45ed-932b-cc0151bc1105%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/CA%2Ba7aJ33WL%2BWcBKoFL5KUP4heZmSZhYnQbrBR_tX8HVsaMmw_g%40mail.gmail.com.
--
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 django-rest-fram...@googlegroups.com.
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.
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
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.
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.
[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
```
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'
]
```
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/CALqbk1QxctvtFSpcsAtczng-H2Xjc9DCBzXC2jNSnycmHTHqpQ%40mail.gmail.com.
http GET 127.0.0.1:8000/api/test/ Authorization:Bearer\ "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1c2VybmFtZSI6ImFkbWluIiwiaWF0IjoxNTc1MTQ3MTkyLCJleHAiOjE1NzUxNDc0OTIsInVzZXJfaWQiOjEsIm9yaWdfaWF0IjoxNTc1MTQ3MTkyfQ.tCDJ3nwe3k5xImxwzePbGitPGV4b_3A1EaF0hkQxzhc"
```
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/CALCmTPF03Ks5bKeZHzbQ7L6XbssdjmvEhtrCPdnru8Z%3DqV8NJA%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/CALqbk1TqBUe351y39eZ%3D%3DWQeLkwXULSK81jCHOWdtFjRPim%3DRA%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/CALCmTPHCq5vfM%2BKmUA8BaE1GOcavOJkSSxDi%2B2bw1G7Ak79kNg%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/CALCmTPHCq5vfM%2BKmUA8BaE1GOcavOJkSSxDi%2B2bw1G7Ak79kNg%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/CA%2Ba7aJ2spbAJED7JXfV_9a-s4xZ0fhp5vR9d_4G-dUyMo4QB-A%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/CALCmTPFaqgLcQBGKj5fHugZeWd9KEkQWJnjtW4zTk8SSaw4BOw%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/CA%2Ba7aJ3A_zU5%2Bo_VuwMgS6o%3DdKR%2BUaDhrkcqyRvWp1fWX08SNw%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/CALCmTPFoBxS0gPPaB7Rw9BcspwSo_rqtzToX3qQieRTnRdS%3DOw%40mail.gmail.com.
Thank you.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/CA%2Ba7aJ1a2qSyi8VAB-rE7AU3Y%3DfiRY_txYLhY6EN25F5_CNmVg%40mail.gmail.com.
Hello, How are you?I am suck with Django Rest framework issue.
I am trying to use `request.user` in Django Rest Framework middleware.
It returns AnonymousUser and I failed.
I have searched and searched but there is no answer.
I wonder if this is Django Rest framework issue.
Who faced this issue? And did you solve?
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 django-rest-fram...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/4c08081d-0706-4b70-b252-63f8ca8f7d17%40googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-rest-framework/CAD1abLxtDke7DV%2BLhsxU5qOaiUdSvaprr7q0%2Bau2C%2BcmbmFiCg%40mail.gmail.com.