Note that there are some explanatory texts on larger screens.

plurals
  1. POAllowing only single active session per user in Django app
    primarykey
    data
    text
    <p>I want to restrict logged-in users to only have one active session, i.e. if the user logs in with a new sessionid, the old session should be terminated. I found a lot of help on SO already: <a href="https://stackoverflow.com/questions/1814437/allow-only-one-concurrent-login-per-user-in-django-app">here</a> and <a href="https://stackoverflow.com/questions/821870/how-can-i-detect-multiple-logins-into-a-django-web-application-from-different-lo">here</a></p> <p>I implemented the middleware solution, with a bit of extra checking...</p> <pre><code>class OnlyOneUserMiddleware(object): """ Middleware to ensure that a logged-in user only has one session active. Will kick out any previous session. """ def process_request(self, request): if request.user.is_authenticated(): try: cur_session_key = request.user.get_profile().session_key if cur_session_key and cur_session_key != request.session.session_key: # Default handling... kick the old session... Session.objects.get(session_key=cur_session_key).delete() if not cur_session_key or cur_session_key != request.session.session_key: p = request.user.get_profile() p.session_key = request.session.session_key p.save() except ObjectDoesNotExist: pass </code></pre> <p>So far, so good... on the Django dev server (manage.py runserver) everything works properly, it kicks the old session...</p> <p>...but when using Apache ( with mod_wsgi), it doesn't work!</p> <p>I've tried to find any information about this, but no luck so far... </p> <p>The closest I have found is <a href="https://stackoverflow.com/questions/4421114/session-issue-with-djangoapachemod-wsgi">this</a>, but it is kind of the 'opposite' problem...</p> <p>Any help would be much appreciated. </p> <p>Edit: I added a debug print before deleting the Session... here's a snippet from Apache's error.log:</p> <pre><code>[Fri Jan 20 09:56:50 2012] [error] old key = f42885ccb7f33b6afcb2c18fca14f44a [Fri Jan 20 09:56:50 2012] [error] new key = ce4cfb672e6025edb8ffcd0cf2b4b8d1 [Fri Jan 20 09:57:14 2012] [error] old key = f42885ccb7f33b6afcb2c18fca14f44a [Fri Jan 20 09:57:14 2012] [error] new key = 0815c56241ac21cf4b14b326f0aa7e24 </code></pre> <p>the first two lies are from when I entered with the first session (Firefox)</p> <p>the last two are from when I entered with the second session (Chromium)</p> <p>... it turns out that the old Session record does not get deleted... ???</p> <p>I'm running vs. the exact same PostgreSQL instance as I did with the devserver...</p> <p>Edit2: It turned out that my code was buggy... it failed when the new Session_key wasn't found in Session...</p> <p>here's the fixed code... the try..except is now in the correct place</p> <pre><code>class OnlyOneUserMiddleware(object): """ Middleware to ensure that a logged-in user only has one session active. Will kick out any previous session. """ def process_request(self, request): if request.user.is_authenticated(): cur_session_key = request.user.get_profile().session_key if cur_session_key and cur_session_key != request.session.session_key: # Default handling... kick the old session... try: s = Session.objects.get(session_key=cur_session_key) s.delete() except ObjectDoesNotExist: pass if not cur_session_key or cur_session_key != request.session.session_key: p = request.user.get_profile() p.session_key = request.session.session_key p.save() </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload