Log out all sessions for current user logged in

1,253 views
Skip to first unread message

Salima Begum

unread,
Feb 16, 2021, 9:54:21 PM2/16/21
to django...@googlegroups.com
Hi all,

Logout all sessions based on current user id and clear django sessions table based on user id. It is logging off all sessions.

```
# Logout all devices in account security page.
def logoff_all(request):
    try:

        # Filtering all session objects.
        session_query = Session.objects.all()

        # Iterating session_query and fetching session key.
        for s in session_query:
            # Decoding session key and getting id from that.
            var_id = s.get_decoded().get('id')
            # Converting string to int data type.
            var_id_user = int(var_id)
            # Id from decode and logged in session id is same then
            # delete that session key from Session table from database.
            if var_id_user == request.session.get('id'):
                s.delete()
        messages.success(request, "All sessions are logged off successfully.")
        return HttpResponseRedirect("/home")
    except Exception as e:
        logging.error(e)
        print(e)
    return HttpResponseRedirect("/home")
```
issue_account.PNG


How to solve this error after logout all devices Please
Thanks
~Salima



Ryan Nowakowski

unread,
Feb 25, 2021, 6:26:11 PM2/25/21
to django...@googlegroups.com
I would call s.flush() instead of s.delete(). That's what Django's
normal logout function does[1].

[1] https://github.com/django/django/blob/a948d9df394aafded78d72b1daa785a0abfeab48/django/contrib/auth/__init__.py#L149

Salima Begum

unread,
Feb 25, 2021, 10:44:09 PM2/25/21
to django...@googlegroups.com
Logout all sessions belong to logged in user Can you please how can I achieve this?

Thanks
~salima

--
You received this message because you are subscribed to the Google Groups "Django users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to django-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/20210225232524.GG19963%40fattuba.com.

Ryan Nowakowski

unread,
Feb 26, 2021, 2:05:36 PM2/26/21
to django...@googlegroups.com
Have your tried my suggestion below? Did it work?

Salima Begum

unread,
Feb 26, 2021, 7:43:40 PM2/26/21
to django...@googlegroups.com
Yes. I have tried that no it is not working and giving error like "Query object doesn't contain flush()"

Ryan Nowakowski

unread,
Feb 27, 2021, 9:15:09 AM2/27/21
to django...@googlegroups.com
Please reply with your new code and copy and paste the exact error message with full traceback.

Salima Begum

unread,
Mar 1, 2021, 12:49:58 AM3/1/21
to django...@googlegroups.com
New code for logging out all sessions of logged in users.

```
# Logout all devices in account security page.
def logoff_all(request):
try:
# Filtering all session objects.
session_query = Session.objects.all()

# Iterating session_query and fetching session key.
for s in session_query:
# Decoding session key and getting id from that.
var_id = s.get_decoded().get('id')
# Converting string to int data type.
var_id_user = int(var_id)
# Id from decode and logged in session id is same then
# delete that session key from Session table from database.
if var_id_user == request.session.get('id'):
                s.flush()

messages.success(request, "All sessions are logged off successfully.")
return HttpResponseRedirect("/home")
except Exception as e:
logging.error(e)
print(e)
return HttpResponseRedirect("/home")
```
**Error**
```
'Session' object has no attribute 'flush'
```

Ryan Nowakowski

unread,
Mar 1, 2021, 10:04:13 AM3/1/21
to django...@googlegroups.com
You might need to call get_decoded then flush on that.

https://docs.djangoproject.com/en/3.1/topics/http/sessions/
Reply all
Reply to author
Forward
0 new messages