Tuesday, February 16, 2021

Log out all sessions for current user logged in

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")
```



How to solve this error after logout all devices Please
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/CAMSz6bk6%3D23CGshE4HRd24TDNf0uS4krv3cHmJbXMgaALQG9Rg%40mail.gmail.com.

No comments:

Post a Comment