#35030 closed New feature (fixed)

Make django.contrib.auth decorators work with async functions.

Reported by: Mike Lissner Owned by: Dingning
Component: contrib.auth Version: dev
Severity: Normal Keywords: async, decorator
Cc: Jon Janzen, Dingning, Ben Lomax Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

We're slowly moving our code base over to async views and tests. Django 5.0 is a big leap forward for this, but we've found that there's still no async version of the @login_required decorator.

We're working around it with some funky decorator usage:

@sync_to_async
@login_required
@async_to_sync
async def our_view(request):

Buuuut, it'd be great not to need to do all this and to either have an async version of login_required (@alogin_required, perhaps?) or for it to just work with async views.

I don't think there's an open issue for this, but amusingly, in a comment 11 months ago, Carlton Gibson touched on making login_required work with async, but he thought it'd take another five years. Perhaps we can do better. :)

Change History (18)

comment:1 by Mariusz Felisiak, 12 months ago

Cc: Jon Janzen Dingning Ben Lomax added
Summary: No async version of login_requiredMake django.contrib.auth decoratos to work with async functions.
Triage Stage: UnreviewedAccepted

Related to #34391 and #31949.

comment:2 by Dingning, 12 months ago

Thank you for CC'ing me. I will study how this function can be implemented, and consider whether I can take on this ticket. I will reply within a day.

comment:3 by Tim Graham, 12 months ago

Summary: Make django.contrib.auth decoratos to work with async functions.Make django.contrib.auth decorators work with async functions.

comment:4 by Dingning, 12 months ago

Owner: changed from nobody to Dingning
Status: newassigned

OK, I have achieved a simple demo, I think I can work on this ticket. I'll I will improve my code further, then submit a PR as soon as possible.

Version 0, edited 12 months ago by Dingning (next)

comment:5 by Dingning, 12 months ago

Has patch: set
Needs documentation: set

comment:6 by Dingning, 12 months ago

Patch needs improvement: set

comment:7 by Natalia Bidart, 12 months ago

Hello Dingning, thanks for your work on this ticket. Once your PR is ready for review, please unset the flags "Patch needs improvement" and "Needs documentation" so it appears in the review queue (docs about this).

comment:8 by Dingning, 12 months ago

Patch needs improvement: unset

comment:9 by Mariusz Felisiak, 12 months ago

Patch needs improvement: set

comment:10 by Dingning, 11 months ago

Hi guys, I created a topic to discuss, welcome to chat here(https://forum.djangoproject.com/t/about-async-version-of-the-login-required-decorator/27077)

comment:11 by Dingning, 11 months ago

Needs documentation: unset
Patch needs improvement: unset

comment:12 by Mariusz Felisiak, 11 months ago

Needs tests: set
Patch needs improvement: set

comment:13 by Dingning, 10 months ago

Needs tests: unset
Patch needs improvement: unset

comment:14 by Dingning, 10 months ago

Hello, dear reviewers. I have updated my commit. Could you please review the pull request?

comment:15 by Mariusz Felisiak, 10 months ago

Patch needs improvement: set

comment:16 by GitHub <noreply@…>, 10 months ago

In c4df2a77:

Refs #35030 -- Added more tests for @user_passes_test decorator.

comment:17 by Mariusz Felisiak, 10 months ago

Patch needs improvement: unset
Triage Stage: AcceptedReady for checkin

comment:18 by Mariusz Felisiak <felisiak.mariusz@…>, 10 months ago

Resolution: fixed
Status: assignedclosed

In 5493209:

Fixed #35030 -- Made django.contrib.auth decorators to work with async functions.

Note: See TracTickets for help on using tickets.
Back to Top