0

i want to use django signals to identify if a user is logged-in twice and if so, revoke his first session so that only one user session can be present at once.

i used the following example but it seems that my signals.py does not even gets reconcnized and i dont know why.

Example: How to prevent multiple login in Django

accounts/signals.py

from django.contrib.auth import user_logged_in
from django.dispatch.dispatcher import receiver
from django.contrib.sessions.models import Session
from .models import UserSession


@receiver(user_logged_in)
def remove_other_sessions(sender, user, request, **kwargs):
    # remove other sessions
    Session.objects.filter(usersession__user=user).delete()

    # save current session
    request.session.save()

    # create a link from the user to the current session (for later removal)
    UserSession.objects.get_or_create(
        user=user,
        session=Session.objects.get(pk=request.session.session_key)
    )

accounts/models.py

# Model to store the list of logged in users
class UserSession(models.Model):
    user = models.ForeignKey(settings.AUTH_USER_MODEL, on_delete=models.CASCADE)
    session = models.OneToOneField(Session, on_delete=models.CASCADE)

accounts/apps.py

from django.apps import AppConfig



class AccountsConfig(AppConfig):
    name = 'Accounts'

    def ready(self):
        import Accounts.signals

but for some reason nothing gets written onto the database for that table.

do i maybe miss something here, this is the very first time i get in touch with signals so i might missed something at the configuration.

Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
  • usually signals are not recognized if you imported app incorrectly. Check out your `INSTALLED_APPS` to have full path, i.e. `.apps.AccountsConfig` – Gasanov May 27 '19 at 15:24
  • Okay, thanks so far, at least now i got a execption :D File "/usr/local/lib/python3.7/site-packages/django/db/models/query.py", line 408, in get app | self.model._meta.object_name app | django.contrib.sessions.models.Session.DoesNotExist: Session matching query does not exist. app | [27/May/2019 15:27:10] "POST /login/ HTTP/1.1" 500 99601 –  May 27 '19 at 15:28
  • Guess your problem is here `Session.objects.get(pk=request.session.session_key)` - for some reason it doesn't exists. Try saving `session.save()` into variable and pass it here. – Gasanov May 27 '19 at 15:34
  • can you please explain: Try saving session.save() into variable and pass it here –  May 27 '19 at 15:42
  • @Venom Have you registered on `__init__.py`? – shaik moeed May 27 '19 at 15:53
  • no, how do i do that? –  May 27 '19 at 15:53
  • @Venom Check the answer and confirm. – shaik moeed May 27 '19 at 16:00

1 Answers1

0

Try this,

default_app_config = 'Accounts.apps.AccountsConfig'

Add this line in __init__.py file of your apps.py file directory.

shaik moeed
  • 5,300
  • 1
  • 18
  • 54
  • I tried but i still get the same error as before: DoesNotExist at /login/ Session matching query does not exist. - /usr/local/lib/python3.7/site-packages/django/db/models/query.py in get, line 408 –  May 27 '19 at 16:11
  • @Venom What is your `LOGIN_URL`? – shaik moeed May 27 '19 at 16:16
  • @Venom You can also follow [this](https://dev.to/fleepgeek/prevent-multiple-sessions-for-a-user-in-your-django-application-13oo) tutorial to avoid multi login – shaik moeed May 27 '19 at 16:21
  • I already tried this post, see: https://stackoverflow.com/questions/56312743/django-allow-only-one-user-session-in-total/56312775#56312775 anyways i will try again, i guess that i simply missed to configure init.py properly –  May 27 '19 at 17:35
  • Okay sure. Try, if you get any queries, you can ask! Happy coding :) – shaik moeed May 27 '19 at 17:39
  • 1
    My mistake was not using the following settings at settings.py: SESSION_ENGINE = "django.contrib.sessions.backends.db" –  May 28 '19 at 10:45