{"id":22927180,"url":"https://github.com/hapytex/django-single-session","last_synced_at":"2025-07-03T05:02:13.950Z","repository":{"id":46169286,"uuid":"515144214","full_name":"hapytex/django-single-session","owner":"hapytex","description":"Allow a user to only have a single active session in Django.","archived":false,"fork":false,"pushed_at":"2024-03-22T10:24:21.000Z","size":63,"stargazers_count":19,"open_issues_count":2,"forks_count":7,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-06-14T08:16:51.057Z","etag":null,"topics":["authentication","django","python","session"],"latest_commit_sha":null,"homepage":"https://pypi.org/project/django-single-session/","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"bsd-3-clause","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hapytex.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null},"funding":{"custom":"https://www.buymeacoffee.com/hapytex"}},"created_at":"2022-07-18T10:51:35.000Z","updated_at":"2025-01-12T17:32:42.000Z","dependencies_parsed_at":"2024-03-22T11:52:32.528Z","dependency_job_id":null,"html_url":"https://github.com/hapytex/django-single-session","commit_stats":{"total_commits":58,"total_committers":3,"mean_commits":"19.333333333333332","dds":"0.18965517241379315","last_synced_commit":"fcd6795ef082df40f36f02b97f92651708fe9b85"},"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/hapytex/django-single-session","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hapytex%2Fdjango-single-session","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hapytex%2Fdjango-single-session/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hapytex%2Fdjango-single-session/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hapytex%2Fdjango-single-session/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hapytex","download_url":"https://codeload.github.com/hapytex/django-single-session/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hapytex%2Fdjango-single-session/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":263264642,"owners_count":23439247,"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":["authentication","django","python","session"],"created_at":"2024-12-14T09:13:53.093Z","updated_at":"2025-07-03T05:02:13.925Z","avatar_url":"https://github.com/hapytex.png","language":"Python","funding_links":["https://www.buymeacoffee.com/hapytex"],"categories":[],"sub_categories":[],"readme":"# Django-single-session\n\n[![PyPi version](https://badgen.net/pypi/v/django-single-session/)](https://pypi.python.org/pypi/django-single-session/)\n[![Documentation Status](https://readthedocs.org/projects/django-single-session/badge/?version=latest)](http://django-single-session.readthedocs.io/?badge=latest)\n[![PyPi license](https://badgen.net/pypi/license/django-single-session/)](https://pypi.python.org/pypi/django-single-session/)\n[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)\n\nA Django app that enforces that a user has only one active session: if the user logs in on another browser/device, then the previous sessions will log out.\n\nThe app will also add an extra action to the `ModelAdmin` of the user model (if there is such `ModelAdmin`), that will alow to log out all sessions of a given (set of) user(s).\n\n## Installation\n\nThe package can be fetched as `django-single-session`, so for example with `pip` with:\n\n```shell\npip3 install django-single-session\n```\n\nOne can install the app by adding the `single_session` app to the `INSTALLED_APPS` setting:\n\n```python3\n# settings.py\n\n# ...\n\nINSTALLED_APPS = [\n    # ...,\n    'django.contrib.sessions',\n    # ...,\n    'single_session'\n    # ...\n]\n\nMIDDLEWARE = [\n    # ...,\n    'django.contrib.sessions.middleware.SessionMiddleware',\n    # ...,\n    'django.contrib.auth.middleware.AuthenticationMiddleware',\n    # ...\n]\n\nSESSION_ENGINE = 'django.contrib.sessions.backends.db'\n```\n\nFor the `SESSION_ENGINE` setting, the database backend, `django.contrib.sessions.backends.db` should be used, since that is the one where the item is linking to.\n\nIn order to work properly, the `SessionMiddleware` and `AuthenticationMiddleware` will be necessary, or another middleware class that will add a `.session` and `.user` attribute on the\nrequest object and will trigger the `user_logged_in` and `user_logged_out` signals with the proper session and user.\n\nand running `migrate` to migrate the database properly:\n\n```shell\npython3 manage.py migrate single_session\n```\n\nThis will by default enforce that a user will only have *one* logged in session. This will *not* proactively logout existing sessions: only if the user logs in with another browser or device,\nthe old session(s) will be closed.\n\n## Configuration\n\nOne can disable the single session behavior by setting `SINGLE_USER_SESSION` in `settings.py` to `False` (or any other value with truthiness `False`).\n\nYou can customise this behaviour by making the `SINGLE_USER_SESSION` setting be a string representing the name of a function which takes a user object as an argument. If this function returns `True` then the user will be logged out. If it returns `False` then the user will not be logged out.\n\nThe tool will also clean up *all* sessions of a user in case that user logs out. This thus means that if a user logs out on one browser/device, they will log out on all other browsers/devices as well. This functionality is still enabled if `SINGLE_USER_SESSION` is set to `False`. You can disable this by setting the `LOGOUT_ALL_SESSIONS` setting in `settings.py` to `False` (or any other value with truthiness `False`).\n\n## Logging out (other) users\n\nIf there is a `ModelAdmin` for the user model (if you use the default user model, then there is such `ModelAdmin`), and the `django.contrib.admin` package is installed,\nthen that `ModelAdmin` will have extra actions to log out normal users and admin users.\n\nYou can thus select users, and log these out with the \"*Log out the user on all sessions*\" action. This will invalidate all the sessions for (all) the selected user(s). In order to do this,\nthe `single_session.logout` permission is required, so only admin users and users with such permission can log out other users. Users with such permission can log out users, but\n*not* admin users.\n\nThere is an extra permission named `single_session.logout_all` to log out all users, including *admin* users. Users with such permission can thus also log out admin users, so it\nmight be better not to give such permission to all (staff) users.\n\n# Contributors\n\n - [@alastair](https://github.com/alastair) implemented a system such that one can add a string that points to a callback for the `SINGLE_USER_SESSION` setting, to make it possible to determine what users should be logged out.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhapytex%2Fdjango-single-session","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhapytex%2Fdjango-single-session","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhapytex%2Fdjango-single-session/lists"}