Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork7k
Using new DJ5.1 LoginRequiredMiddleware with login_not_required erroneously redirects API call to login-page#9503
-
Using the new Django 5.1 Moving the I'm of the opinion API-calls should never redirect to a login-page, but rather return the appropriate http-statuscode. Very interested in your opinions/solutions. |
BetaWas this translation helpful?Give feedback.
All reactions
Replies: 4 comments
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
-
I'm not sure this is the best solution, but you can create a custom middleware that bypasses # app/middleware.pyfromdjango.confimportsettingsfromdjango.contrib.auth.middlewareimportLoginRequiredMiddlewarefromdjango.utils.deprecationimportMiddlewareMixinimportreclassCustomLoginRequiredMiddleware(LoginRequiredMiddleware):def__init__(self,get_response=None):self.get_response=get_responseself.open_urls= [re.compile(url)forurlinsettings.OPEN_URLS]super().__init__(get_response)defprocess_view(self,request,view_func,view_args,view_kwargs):forurlinself.open_urls:ifurl.match(request.path):returnNone# Pass through, no login requiredreturnsuper().process_view(request,view_func,view_args,view_kwargs) In # app/settings.pyMIDDLEWARE= [# ..."app.middleware.CustomLoginRequiredMiddleware",]# Regex patterns for paths that bypass LoginRequiredMiddlewareOPEN_URLS= [r"^/my-api/.*",# ...] I'm also very interested in opinions and solutions. |
BetaWas this translation helpful?Give feedback.
All reactions
-
As far as I understand, it's because Django django-rest-framework/rest_framework/views.py Lines 385 to 397 in337ba21
django-rest-framework/rest_framework/request.py Lines 378 to 395 in8e304e1
I would be curious if you see the same behaviour with session auth. My expectation is that it would work, because this relies on a Django built-in auth mechanism, while the others (Basic and token based auth) are DRF specific. With regards to solutions, one might be for DRF to provide a specialized version of Django's |
BetaWas this translation helpful?Give feedback.
All reactions
-
I've dug this a bit more to attempt to add compatibility to DRF as part of#9514 and just realised that DRF already offers a way to make sure all endpoints are authenticated, via the |
BetaWas this translation helpful?Give feedback.
All reactions
-
@browniebroke Came accross this, since I was thinking weather I should be using DRF |
BetaWas this translation helpful?Give feedback.