{"id":19936045,"url":"https://github.com/imbolc/async-django-user","last_synced_at":"2025-07-09T13:37:30.549Z","repository":{"id":57412086,"uuid":"217669138","full_name":"imbolc/async-django-user","owner":"imbolc","description":"Using django user with async frameworks like aiohttp, starlette, etc.","archived":false,"fork":false,"pushed_at":"2019-11-16T05:58:24.000Z","size":28,"stargazers_count":11,"open_issues_count":2,"forks_count":3,"subscribers_count":3,"default_branch":"master","last_synced_at":"2024-04-14T01:00:18.444Z","etag":null,"topics":["aiohttp","asyncio","authorization","django","starlette"],"latest_commit_sha":null,"homepage":"","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/imbolc.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2019-10-26T06:58:33.000Z","updated_at":"2022-05-17T22:41:24.000Z","dependencies_parsed_at":"2022-09-09T17:22:04.618Z","dependency_job_id":null,"html_url":"https://github.com/imbolc/async-django-user","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imbolc%2Fasync-django-user","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imbolc%2Fasync-django-user/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imbolc%2Fasync-django-user/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/imbolc%2Fasync-django-user/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/imbolc","download_url":"https://codeload.github.com/imbolc/async-django-user/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252191070,"owners_count":21709005,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aiohttp","asyncio","authorization","django","starlette"],"created_at":"2024-11-12T23:23:08.340Z","updated_at":"2025-05-03T12:31:35.217Z","avatar_url":"https://github.com/imbolc.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"Async Django User\n=================\n\nUsing [django][] user with async frameworks like [aiohttp][], [starlette][] etc.\n\n    pip install async-django-session async-django-user\n\ntl;dr\n-----\nTake a look at registration / authorization examples for\n[aiohttp + databases][aiohttp example]\nor [starlette + asyncpg][starlette example].\n\nAPI\n---\n\n### Backends\n\nThere's two ways of communicating to database available:\n\n- through [databases][] - which is compatible with most of major RDBMS:\n    ```python\n    database = databases.Database(DB_URI)\n    await database.connect()\n    backend = async_django_user.databases.Backend(database, SECRET_KEY)\n    ```\n- or directly through [asyncpg][] (PostgreSQL only):\n    ```python\n    pool = await asyncpg.create_pool(DB_URI)\n    backend = async_django_user.asyncpg.Backend(pool, SECRET_KEY)\n    ```\n\n### User\n\nTo fetch an user from db by its id stored in [django session] there's\n`backend.get_user_from_session` method:\n```python\nuser = backend.get_user_from_session(session)\n```\nIt's lazy so the user data won't be actually fetched until you call its\n`load` method. It caches the result, so it's inexpensive to call it multiple\ntimes:\n```python\nawait user.load()\n```\n\nUser provides dict interface to it's data (eg `user[\"username\"]`) and a few\nmethods:\n- `await user.authenticate(username, password)` - checks credentials and populates\n  the user from database if they're valid\n- `user.login()` - sets session variables logging the user in\n- `user.logout()` - clears the session data\n- `await user.set_password(password)` - sets a new password for the user\n- `await user.save([fields])` - saves the whole user or a particular set of its\n   fields\n- `await register()` - saves a new user into db\n\nFrameworks integration\n----------------------\nThere's built-in middlewares for a few async frameworks to automatically load\nuser of the current request. Take a look at [examples][] folder for:\n- [aiohttp example][] with [databases backend][]\n- [starlette example][] with [asyncpg backend][]\n\n\nRunning examples\n----------------\nRunning the [examples][] you can see different frameworks using the same session\nand user data.\n\nInstall the requirements:\n\n    cd examples\n    pip install -r requirements.txt\n\nCreate database and tables:\n\n    createdb async_django_session\n    python django_app.py migrate\n\nCreate a user:\n\n    python django_app.py createsuperuser\n\nRun [aiohttp example][] which uses [databases backend][]:\n\n    python aiohttp_app.py\n\nRun [starlette example][] which uses [asyncpg backend][]:\n\n    python starlette_app.py\n\nRun [django example][]:\n\n    python django_app.py runserver\n\n[aiohttp]: https://github.com/aio-libs/aiohttp\n[starlette]: https://github.com/encode/starlette\n[asyncpg]: https://github.com/MagicStack/asyncpg\n[databases]: https://github.com/encode/databases\n[django]: https://github.com/django/django\n[examples]: https://github.com/imbolc/async-django-user/tree/master/examples\n[django example]: https://github.com/imbolc/async-django-user/tree/master/examples/django_app.py\n[starlette example]: https://github.com/imbolc/async-django-user/tree/master/examples/starlette_app.py\n[aiohttp example]: https://github.com/imbolc/async-django-user/tree/master/examples/aiohttp_app.py\n[asyncpg backend]: https://github.com/imbolc/async-django-user/tree/master/async-django-user/asyncpg.py\n[databases backend]: https://github.com/imbolc/async-django-user/tree/master/async-django-user/databases.py\n[aiohttp middleware]: https://github.com/imbolc/async-django-user/tree/master/async-django-user/aiohttp.py\n[starlette middleware]: https://github.com/imbolc/async-django-user/tree/master/async-django-user/starlette.py\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimbolc%2Fasync-django-user","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fimbolc%2Fasync-django-user","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fimbolc%2Fasync-django-user/lists"}