Get username in a middleware from Django Rest Framework SIMPLE JWT token (3rd party)
Asked Answered
F

2

6

I am using Django Rest Framework and I've included a 3rd party package called REST framework simple JWT Auth which is the new framework referenced, and this one, REST framework JWT Auth, which is the old one (I Imagine), since there was no update on github since a long time and maybe not supported for newer versions.

And I'm looking for a way, like this link on stackoverflow-3rd answer, via middlewares, to get the user information for each request in order to apply/save it, in needed, the user object in my models by using django signals. I checked in documentation and on internet, but I didn't find anything. So, if you already had that case, I will appreciate your help.

Thank you

Frodin answered 1/3, 2019 at 13:41 Comment(0)
T
21

To get username from the user model you should use request.user. This will give you the authenticated user's info with the request. However if you use simple_jwt, it's not possible to directly use request.user in a middleware because authentication mechanism works in view function.

So you should manually authenticate inside the middleware and then you can use request.user to get whatever data from the user model.

from rest_framework_simplejwt import authentication


class MyMiddleware():
    def __init__(self, get_response):
        self.get_response = get_response

    def __call__(self, request):
        response = self.get_response(request)
        return response

    def process_view(self, request, view_func, view_args, view_kwargs):
        request.user = authentication.JWTAuthentication().authenticate(request)[0]  # Manually authenticate the token

        print(request.user)  # Use the request.user as you want
Tisdale answered 22/7, 2019 at 8:36 Comment(1)
Upvote for being the only one that actually puts the user onto the request object (and therefore the only correct answer, IMO). However, it's worth noting that this should be considered a workaround, since it ends up in the authentication code being called twice... thus it's preferable to alter code to do what's needed in the actual view rather than in the middleware.Rowe
L
4

With simple_jwt the user information will not be set on the request initially but after the request is processed it will be. So if you're just logging the information and you can do it after the request is processed, do it there.

So for example in a simple middleware:

def simple_middleware(get_response):
    def middleware(request):
        # request.user is an anonymous user at this point
        response = get_response(request)
        # request.uesr is the logged-in user here
        return response
    return middleware
Longhorn answered 11/5, 2022 at 14:24 Comment(2)
I believe this answer to be just as valid as the more upvoted one. This solved my problem, because I needed the user information in a piece of middleware. This achieves that (and I don't know if I'd ever figure out to call get_response() first on my own)Positivism
Much cleaner, thanksBuyers

© 2022 - 2024 — McMap. All rights reserved.