Djangorestframework-simplejwt: how get request.user in middleware, it show AnonymousUser always

Created on 12 Jan 2021  路  5Comments  路  Source: jazzband/djangorestframework-simplejwt

May be I should ask that where request.user=Auth...

question

Most helpful comment

@zhangxu3486432 What middleware? We include a middleware? You need to paste your settings configuration for SimpleJWT because otherwise I can't understand what's happening. Please also provide steps so that I can also reproduce the error.

There is a case that we want to get user info in our custom middleware, but django-rest-framework-simplejwt assign user to request after the middleware methods was executed. Thus, JWTAuthentication().authenticate(request) will execute twice.

All 5 comments

Are you logging in properly? Test with the DRF UI to see if you can access a view that has permission class to only allow logged in users.

request.user = JWTAuthentication().authenticate(request)[0]

Try code above, if you subclass JWTAuthentication, replace JWTAuthentication with your subclass authentication Class.

request.user = JWTAuthentication().authenticate(request)[0]

Try code above, if you subclass JWTAuthentication, replace JWTAuthentication with your subclass authentication Class.

I want to use the authentication by simplejwt directly, not do it again.

But simplejwt to do authentication after the middleware?

I'm not sure.

@zhangxu3486432 What middleware? We include a middleware? You need to paste your settings configuration for SimpleJWT because otherwise I can't understand what's happening. Please also provide steps so that I can also reproduce the error.

@zhangxu3486432 What middleware? We include a middleware? You need to paste your settings configuration for SimpleJWT because otherwise I can't understand what's happening. Please also provide steps so that I can also reproduce the error.

There is a case that we want to get user info in our custom middleware, but django-rest-framework-simplejwt assign user to request after the middleware methods was executed. Thus, JWTAuthentication().authenticate(request) will execute twice.

Was this page helpful?
0 / 5 - 0 ratings